Skip to content

Instantly share code, notes, and snippets.

@isaacs
Last active February 9, 2017 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/0101c2e0b35f4c7014f7f09f390abf31 to your computer and use it in GitHub Desktop.
Save isaacs/0101c2e0b35f4c7014f7f09f390abf31 to your computer and use it in GitHub Desktop.
// testing converting arguments to an Array
function manualMap () {
var l = arguments.length
var arr = new Array(l)
for (var i = 0; i < l; i ++) arr[i] = arguments[i]
return arr
}
function manualMapArg (x) {
var l = arguments.length
var arr = new Array(l)
for (var i = 0; i < l; i ++) arr[i] = arguments[i]
return arr
}
function duff () {
var l = arguments.length
var arr = new Array(l)
switch (l) {
case 8: arr[7] = arguments[7]
case 7: arr[6] = arguments[6]
case 6: arr[5] = arguments[5]
case 5: arr[4] = arguments[4]
case 4: arr[3] = arguments[3]
case 3: arr[2] = arguments[2]
case 2: arr[1] = arguments[1]
case 1: arr[0] = arguments[0]
}
return arr
}
function from () {
return Array.from(arguments)
}
function arrayApplyProto () {
return Array.apply(Object.create(Array.prototype), arguments)
}
function arrayApplyNull () {
return arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)
}
function arrayApplyArg (x) {
return arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)
}
function sliceCall () {
return Array.prototype.slice.call(arguments)
}
function sliceCallArg (x) {
return Array.prototype.slice.call(arguments)
}
function sliceZeroCall () {
return Array.prototype.slice.call(arguments, 0)
}
function sliceZeroCallArg (x) {
return Array.prototype.slice.call(arguments, 0)
}
function sliceApply () {
return Array.prototype.slice.apply(arguments)
}
function sliceApplyArg (x) {
return Array.prototype.slice.apply(arguments)
}
function sliceZeroApply () {
return Array.prototype.slice.apply(arguments, [0])
}
function sliceZeroApplyArg (x) {
return Array.prototype.slice.apply(arguments, [0])
}
function arrayProto () {
arguments.__proto__ = Array.prototype
return arguments
}
function splat (...args) {
return args
}
exports.compare =
{ manualMap: function () {
return [ manualMap(Math.random()), manualMap(Math.random(), Math.random()) ]
}
, splat: function () {
return [ splat(Math.random()), splat(Math.random(), Math.random()) ]
}
, arrayProto: function () {
return [ arrayProto(Math.random()), arrayProto(Math.random(), Math.random()) ]
}
, duff: function () {
return [ duff(Math.random()), duff(Math.random(), Math.random()) ]
}
, arrayApplyNull: function () {
return [ arrayApplyNull(Math.random()), arrayApplyNull(Math.random(), Math.random()) ]
}
, manualMapArg: function () {
return [ manualMapArg(Math.random()), manualMapArg(Math.random(), Math.random()) ]
}
, arrayApplyArg: function () {
return [ arrayApplyArg(Math.random()), arrayApplyArg(Math.random(), Math.random()) ]
}
, sliceCall: function () {
return [ sliceCall(Math.random()), sliceCall(Math.random(), Math.random()) ]
}
, sliceCallArg: function () {
return [ sliceCallArg(Math.random()), sliceCallArg(Math.random(), Math.random()) ]
}
, sliceZeroCall: function () {
return [ sliceZeroCall(Math.random()), sliceZeroCall(Math.random(), Math.random()) ]
}
, sliceZeroCallArg: function () {
return [ sliceZeroCallArg(Math.random()), sliceZeroCallArg(Math.random(), Math.random()) ]
}
, sliceApply: function () {
return [ sliceApply(Math.random()), sliceApply(Math.random(), Math.random()) ]
}
, sliceApplyArg: function () {
return [ sliceApplyArg(Math.random()), sliceApplyArg(Math.random(), Math.random()) ]
}
, sliceZeroApply: function () {
return [ sliceZeroApply(Math.random()), sliceZeroApply(Math.random(), Math.random()) ]
}
, sliceZeroApplyArg: function () {
return [ sliceZeroApplyArg(Math.random()), sliceZeroApplyArg(Math.random(), Math.random()) ]
}
}
exports.countPerLap = 2
require("../").runMain()
benchmarking /Users/isaacs/dev/js/bench/examples/array-ify.js
Please be patient.
{ http_parser: '2.7.0',
node: '8.0.0-pre',
v8: '5.5.372.40',
uv: '1.10.2',
zlib: '1.2.8',
ares: '1.10.1-DEV',
modules: '52',
openssl: '1.0.2k',
icu: '58.2',
unicode: '9.0',
cldr: '30.0.3',
tz: '2016j' }
Scores: (bigger is better)
manualMap
Raw:
> 42559.44055944056
> 43014.98501498502
> 43156.843156843155
> 43244.755244755244
Average (mean) 42994.005994005995
duff
Raw:
> 46215.78421578422
> 38681.318681318684
> 42429.57042957043
> 37818.181818181816
Average (mean) 41286.21378621379
splat
Raw:
> 38399.6003996004
> 37670.32967032967
> 38899.1008991009
> 34621.37862137862
Average (mean) 37397.6023976024
manualMapArg
Raw:
> 21992.00799200799
> 29142.85714285714
> 29153.692614770458
> 25732.267732267734
Average (mean) 26505.206370475833
arrayApplyNull
Raw:
> 10325.674325674325
> 10081.918081918082
> 9296.703296703297
> 9586.413586413586
Average (mean) 9822.677322677322
arrayApplyArg
Raw:
> 9356.643356643357
> 9344.655344655344
> 7850.14985014985
> 9098.901098901099
Average (mean) 8912.587412587412
sliceApply
Raw:
> 6769.2307692307695
> 6929.070929070929
> 5508.491508491508
> 6711.288711288711
Average (mean) 6479.520479520479
sliceZeroApply
Raw:
> 6285.714285714285
> 6427.572427572428
> 6225.774225774226
> 6367.632367632367
Average (mean) 6326.673326673326
sliceCall
Raw:
> 6825.174825174825
> 5830.339321357285
> 6047.952047952048
> 6423.576423576424
Average (mean) 6281.760654515145
sliceCallArg
Raw:
> 6115.884115884116
> 5834.165834165834
> 5858.141858141858
> 6057.9420579420575
Average (mean) 5966.533466533467
arrayProto
Raw:
> 5836.163836163836
> 5668.331668331668
> 5982.017982017982
> 5924.075924075924
Average (mean) 5852.647352647353
sliceApplyArg
Raw:
> 6079.92007992008
> 5272.727272727273
> 5802.197802197802
> 5810.1898101898105
Average (mean) 5741.258741258742
sliceZeroCall
Raw:
> 5202.797202797203
> 6551.448551448551
> 5230.7692307692305
> 5970.02997002997
Average (mean) 5738.761238761238
sliceZeroCallArg
Raw:
> 4893.106893106893
> 5680.319680319681
> 5664.335664335665
> 5496.5034965034965
Average (mean) 5433.566433566434
sliceZeroApplyArg
Raw:
> 5354.645354645355
> 5102.897102897103
> 5666.333666333667
> 5442.557442557442
Average (mean) 5391.608391608392
Winner: manualMap
Compared with next highest (duff), it's:
3.97% faster
1.04 times as fast
0.02 order(s) of magnitude faster
A LITTLE FASTER
Compared with the slowest (sliceZeroApplyArg), it's:
87.46% faster
7.97 times as fast
0.9 order(s) of magnitude faster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment