Last active
January 13, 2017 14:24
-
-
Save syaau/bb328aead4943f7d8a2e156f46845d16 to your computer and use it in GitHub Desktop.
nodejs array spread operator benchmarking
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
/** | |
* Benchmarking the array modification for redux using different techniques | |
*/ | |
const ITERATIONS = 500000; | |
// Create an array with the given length | |
function range(length) { | |
const list = []; | |
for (let i = 0; i < length; i += 1) { | |
list.push(Math.random()); | |
} | |
return list; | |
} | |
// Perform the operation and time it | |
function clock(title, operation, size) { | |
const start = Date.now(); | |
for (let i = 0; i < ITERATIONS; i += 1) { | |
if (operation(i).length !== size) { | |
// Just making sure if there had been any error in performing the operation | |
console.error('Error'); | |
} | |
} | |
const duration = Date.now() - start; | |
console.log(` ${title} ${(duration * 1000) / ITERATIONS} microseconds per operation`); | |
} | |
// The main benchmarking routine | |
function benchmark(arraySize, index) { | |
const list = range(arraySize); | |
console.log(`Running benchmark with array size ${arraySize} and index ${index}`); | |
clock('Array clone with slice', () => list.slice(), arraySize); | |
clock('Array clone with spread operator', () => [...list], arraySize); | |
clock('Array addition with plain concat', n => list.concat(n), arraySize + 1); | |
clock('Array addition with spread operator', n => [...list, n], arraySize + 1); | |
clock('Array addition with slice and push', (n) => { | |
const clone = list.slice(); | |
clone.push(n); | |
return clone; | |
}, arraySize + 1); | |
clock('Array removal with slice and concat', () => ( | |
list | |
.slice(0, index) | |
.concat(list.slice(index + 1)) | |
), arraySize - 1); | |
clock('Array removal with spread operator', () => ( | |
[ | |
...list.slice(0, index), | |
...list.slice(index + 1), | |
] | |
), arraySize - 1); | |
clock('Array removal with slice and splice', () => { | |
const clone = list.slice(); | |
clone.splice(index, 1); | |
return clone; | |
}, arraySize - 1); | |
clock('Array update with slice and concat', n => ( | |
list | |
.slice(0, index) | |
.concat([n]) | |
.concat(list.slice(index + 1)) | |
), arraySize); | |
clock('Array update with spread operator', n => ( | |
[ | |
...list.slice(0, index), | |
n, | |
...list.slice(index + 1), | |
] | |
), arraySize); | |
clock('Array update with slice and assign', (n) => { | |
const clone = list.slice(); | |
clone[index] = n; | |
return clone; | |
}, arraySize); | |
} | |
// Run benchmark with an array of size 500 and update/removal at mid point | |
benchmark(500, 250); | |
// Run benchmark with an array of size 10 and update/removal at mid point | |
benchmark(10, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment