Created
September 24, 2014 05:19
-
-
Save JacksonTian/afefb01b5534814be508 to your computer and use it in GitHub Desktop.
push.apply
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Benchmark = require('benchmark'); | |
var suite = new Benchmark.Suite(); | |
var headers0 = []; | |
var headers1 = ['k1', 'v1']; | |
var headers = ['k1', 'v1', 'k1', 'v1', 'k1', 'v1', 'k1', 'v1', 'k1', 'v1', 'k1', 'v1']; | |
// add tests | |
suite.add('apply 0', function () { | |
var raw = []; | |
raw.push.apply(raw, headers0); | |
}) | |
.add('push 0', function () { | |
var raw = []; | |
for (var i = 0; i < headers0.length; i = i+2) { | |
raw.push(headers0[i]); | |
raw.push(headers0[i + 1]); | |
} | |
}) | |
.add('apply 1', function () { | |
var raw = []; | |
raw.push.apply(raw, headers1); | |
}) | |
.add('push 1', function () { | |
var raw = []; | |
for (var i = 0; i < headers1.length; i = i+2) { | |
raw.push(headers1[i]); | |
raw.push(headers1[i + 1]); | |
} | |
}) | |
.add('apply n', function () { | |
var raw = []; | |
raw.push.apply(raw, headers); | |
}) | |
.add('push n', function () { | |
var raw = []; | |
for (var i = 0; i < headers.length; i = i+2) { | |
raw.push(headers[i]); | |
raw.push(headers[i + 1]); | |
} | |
}) | |
.add('push nn', function () { | |
var raw = []; | |
for (var i = 0; i < headers.length; i = i+2) { | |
raw.push(headers[i], headers[i + 1]); | |
} | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
// run async | |
.run({ 'async': true }); | |
// jacksontian node_research $ node push.js | |
// apply 0 x 9,237,973 ops/sec ±1.80% (90 runs sampled) | |
// push 0 x 45,320,843 ops/sec ±1.95% (83 runs sampled) | |
// apply 1 x 4,803,977 ops/sec ±2.43% (82 runs sampled) | |
// push 1 x 18,637,590 ops/sec ±2.41% (76 runs sampled) | |
// apply n x 3,523,985 ops/sec ±1.93% (85 runs sampled) | |
// push n x 10,443,368 ops/sec ±1.88% (90 runs sampled) | |
// push nn x 2,122,618 ops/sec ±1.59% (86 runs sampled) | |
// Fastest is push 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment