JavaScript - performance / time measurement of function calls

You can use performance.now():

1
2
3
4
5
6
7
8
9
10
11
const t0 = performance.now();
const arr1 = [1, 2, 3, 4, 5];
arr1.unshift(0);
const t1 = performance.now();
console.log(t1 - t0);

const t2 = performance.now();
const arr2 = [1, 2, 3, 4, 5];
const arr3 = [0, ...arr2];
const t3 = performance.now();
console.log(t3 - t2);

Or console.time and console.timeEnd:

1
2
3
4
5
6
7
8
9
console.time('unshift');
const arr1 = [1, 2, 3, 4, 5];
arr1.unshift(0)
console.timeEnd('unshift');

console.time('destructuring');
const arr2 = [1, 2, 3, 4, 5];
const arr3 = [0, ...arr2];
console.timeEnd('destructuring');