Skip to content

Instantly share code, notes, and snippets.

@Hypercubed
Last active August 29, 2015 14:10
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 Hypercubed/65c00b905fecd83e674e to your computer and use it in GitHub Desktop.
Save Hypercubed/65c00b905fecd83e674e to your computer and use it in GitHub Desktop.
PowerArray benchmarks
node_modules/
var Benchmark = require('benchmark');
var PowerArray = require('PowerArray'),
fast = require('fast.js'),
_ = require('underscore')
lodash = require('lodash');
var i,
LEN = 1e7,
array = Array();
for (i=0; i < LEN; i++) {
var rnd = Math.floor(Math.random() * 100 + 1);
array.push(rnd);
var p = i/LEN*100;
if (p % 10 === 0) {
console.log(p+'% complete');
}
}
var parray = new PowerArray(array);
var iarray = new Uint16Array(array);
var suite = new Benchmark.Suite;
suite
.add('array.forEach', function() {
array.forEach(function (i) {
i * 2;
});
})
.add('Array.prototype.forEach on array', function() {
Array.prototype.forEach.call(array,function (i) {
i * 2;
});
})
.add('Array.prototype.forEach on Uint16Array', function() {
Array.prototype.forEach.call(iarray,function (i) {
i * 2;
});
})
.add('Array.prototype.forEach on PowerArray', function() {
Array.prototype.forEach.call(parray, function (i) {
i * 2;
});
})
.add('powerArray.forEach', function() {
parray.forEach(function (i) {
i * 2;
});
})
.add('PowerArray.prototype.forEach on Uint16Array', function() {
PowerArray.prototype.forEach.call(iarray, function (i) {
i * 2;
});
})
.add('fast.js on Array', function() {
fast.forEach(array,function (i) {
i * 2;
});
})
/*.add('fast.js on Uint16Array', function() { // fast.js treats non-instanceof Arrays as objects
fast.forEach(iarray,function (i) {
i * 2;
});
}) */
.add('fast.js on PowerArray', function() {
fast.forEach(parray,function (i) {
i * 2;
});
})
.add('underscore on Array', function() {
_.forEach(array,function (i) {
i * 2;
});
})
.add('underscore on Uint16Array', function() {
_.forEach(iarray,function (i) {
i * 2;
});
})
.add('lodash on Array', function() {
lodash.forEach(array,function (i) {
i * 2;
});
})
.add('lodash on Uint16Array', function() {
lodash.forEach(iarray,function (i) {
i * 2;
});
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
.run({ 'async': true });
array.forEach x 1.50 ops/sec ±3.77% (8 runs sampled)
Array.prototype.forEach on array x 1.46 ops/sec ±7.01% (8 runs sampled)
Array.prototype.forEach on Uint16Array x 1.54 ops/sec ±3.10% (8 runs sampled)
Array.prototype.forEach on PowerArray x 1.42 ops/sec ±4.60% (8 runs sampled)
powerArray.forEach x 5.13 ops/sec ±4.00% (18 runs sampled)
PowerArray.prototype.forEach on Uint16Array x 4.83 ops/sec ±2.44% (17 runs sampled)
fast.js on Array x 5.21 ops/sec ±2.97% (18 runs sampled)
fast.js on PowerArray x 5.16 ops/sec ±3.60% (17 runs sampled)
underscore on Array x 5.22 ops/sec ±1.82% (17 runs sampled)
underscore on Uint16Array x 5.13 ops/sec ±1.29% (17 runs sampled)
lodash on Array x 5.29 ops/sec ±1.48% (18 runs sampled)
lodash on Uint16Array x 4.93 ops/sec ±3.50% (17 runs sampled)
Fastest is lodash on Array,underscore on Array,fast.js on Array,fast.js on PowerArray,powerArray.forEach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment