Created
July 19, 2017 22:04
-
-
Save liath/db727a85214018bcbaa9c61537a05160 to your computer and use it in GitHub Desktop.
Benchmark node.js array concatenation methods
This file contains 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
const Benchmark = require('benchmark'); | |
const _ = require('lodash'); | |
const assert = require('assert'); | |
const saved = []; | |
for (let i = 0; i < 400; i += 1) { | |
saved.push(i); | |
} | |
// Uncomment the asserts to verify that all tests are generating the same outputs | |
const goldStandard = saved.concat(saved); | |
let array1 = []; | |
let array2 = []; | |
let array3 = []; | |
const nonEmptyTemp = [1, 2]; | |
const reset = () => { | |
array1 = saved; | |
array2 = saved; | |
array3 = []; | |
}; | |
const methods = { | |
assignSpreadPush: () => { | |
reset(); | |
array3 = [...array1]; | |
array3.push(...array2); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
concat: () => { | |
reset(); | |
array3 = array1.concat(array2); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
concatSmall: () => { | |
reset(); | |
array3 = [].concat(array1, array2); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
concatSmallInit: () => { | |
reset(); | |
array3 = nonEmptyTemp.concat(array1, array2).slice(2); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
concatSpreadSmallInit: () => { | |
reset(); | |
array3 = [...[1, 2], ...array1, ...array2].slice(2); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
push: () => { | |
reset(); | |
array1.forEach(x => array3.push(x)); | |
array2.forEach(x => array3.push(x)); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
pushClone: () => { | |
reset(); | |
array3 = array1.slice(0); | |
array2.forEach(x => array3.push(x)); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
sliceSpreadPush: () => { | |
reset(); | |
array3 = array1.slice(0); | |
array3.push(...array2); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
spread: () => { | |
reset(); | |
array3 = [...array1, ...array2]; | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
spreadInital: () => { | |
reset(); | |
array3 = [1, 2, ...array1, ...array2].slice(2); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
spreadPush: () => { | |
reset(); | |
array3.push(...array1); | |
array3.push(...array2); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
spreadPushInlineInline: () => { | |
reset(); | |
array3.push(...array1, ...array2); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
spreadPushInlineInlineInnerArray: () => { | |
reset(); | |
array3.push(...[...array1, ...array2]); | |
// assert.ok(_.isEqual(array3, goldStandard)); | |
}, | |
}; | |
console.log('Tests running...\n'); | |
const suite = new Benchmark.Suite(); | |
_.each(methods, (f, n) => suite.add(n, f)); | |
suite.on('cycle', event => { | |
console.log(String(event.target)); | |
console.log(`Memory: ${process.memoryUsage().heapUsed / 1024 / 1024} Mb\n`); | |
}) | |
.on('complete', function complete() { | |
console.log(`Fastest is ${this.filter('fastest').map('name')}`); // eslint-disable-line | |
}) | |
.run({}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment