Skip to content

Instantly share code, notes, and snippets.

@preco21
Last active August 5, 2016 03:22
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 preco21/382f86c062c29b778dc6035c88caf1a2 to your computer and use it in GitHub Desktop.
Save preco21/382f86c062c29b778dc6035c88caf1a2 to your computer and use it in GitHub Desktop.
Benchmark various methods to copy array in ES2015
import {Suite} from 'benchmark';
const suite = new Suite();
suite
.add('Array Spread Operator', () => {
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const b = [...a];
})
.add('Array.from()', () => {
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const b = Array.from(a);
})
.add('arr.slice()', () => {
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const b = a.slice();
})
.add('arr.concat()', () => {
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const b = a.concat();
})
.on('cycle', (event) => {
console.log(String(event.target));
})
.on('complete', () => {
console.log(`Winner: ${suite.filter('fastest').map('name')}`);
})
.run({async: true});
@preco21
Copy link
Author

preco21 commented Apr 27, 2016

Results:

Array Spread Operator x 420,984 ops/sec ±2.48% (81 runs sampled)
Array.from() x 948,784 ops/sec ±2.36% (81 runs sampled)
arr.slice() x 6,755,822 ops/sec ±3.02% (86 runs sampled)
arr.concat() x 5,630,838 ops/sec ±2.32% (90 runs sampled)
Winner: arr.slice()

OS: Windows 10
Node: 5.11.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment