Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trickpattyFH20/7a907a2a24a7a4341f6f8bd953333855 to your computer and use it in GitHub Desktop.
Save trickpattyFH20/7a907a2a24a7a4341f6f8bd953333855 to your computer and use it in GitHub Desktop.
"use strict";
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
// add tests
suite
.add('for', function() {
const a = [1, 2, 3];
const b = [4, 5, 6];
// optimized for loop
const len = b.length;
for(let i = 0; i < len; i++) {
a.push(b[i]);
}
})
.add('forOf', function() {
const a = [1, 2, 3];
const b = [4, 5, 6];
for(let val of b) {
a.push(val);
}
})
.add('pushApply', function() {
const a = [1, 2, 3];
const b = [4, 5, 6];
Array.prototype.push.apply(a, b);
})
.add('pushSpread', function() {
const a = [1, 2, 3];
const b = [4, 5, 6];
a.push(...b);
})
.add('concat', function() {
const a = [1, 2, 3];
const b = [4, 5, 6];
const c = a.concat(b);
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({ 'async': true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment