Skip to content

Instantly share code, notes, and snippets.

@LarsVonQualen
Created October 20, 2016 09:19
Show Gist options
  • Save LarsVonQualen/7c36d331076eb4f80f4703bce494d802 to your computer and use it in GitHub Desktop.
Save LarsVonQualen/7c36d331076eb4f80f4703bce494d802 to your computer and use it in GitHub Desktop.
function test(iterations) {
console.log(`Testing with ${iterations} iterations`);
const originalArray = [];
console.time('data');
for (let i = 0; i < 1000; i++) {
originalArray.push(i);
}
console.timeEnd('data');
console.time('slice');
let sliceTmp;
for (let i = 0; i < iterations; i++) {
sliceTmp = originalArray.slice(0);
}
console.timeEnd('slice');
console.time('spread');
let spreadTmp;
for (let i = 0; i < iterations; i++) {
spreadTmp = [...originalArray];
}
console.timeEnd('spread');
}
test(1);
test(10);
test(100);
test(1000);
test(10000);
test(100000);
test(1000000);
// OUTPUT
> Testing with 1 iterations
> data: 0.084ms
> slice: 0.413ms
> spread: 0.48ms
> Testing with 10 iterations
> data: 0.0859ms
> slice: 0.217ms
> spread: 2.91ms
> Testing with 100 iterations
> data: 0.075ms
> slice: 5.86ms
> spread: 33.3ms
> Testing with 1000 iterations
> data: 0.05ms
> slice: 25.1ms
> spread: 159ms
> Testing with 10000 iterations
> data: 0.0261ms
> slice: 21.2ms
> spread: 1230ms
> Testing with 100000 iterations
> data: 0.0259ms
> slice: 125ms
> spread: 12000ms
> Testing with 1000000 iterations
> data: 0.0352ms
> slice: 1200ms
> spread: 120000ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment