-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathd-copy-array.js
More file actions
45 lines (34 loc) · 718 Bytes
/
Copy pathd-copy-array.js
File metadata and controls
45 lines (34 loc) · 718 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const benchmark = require('./2-benchmark.js');
// Prepare data
const arr = [];
for (let i = 0; i < 100; i++) {
arr[i] = (i - 50) * i;
}
// Different implementations
const testSlice = () => arr.slice();
const testSlice0 = () => arr.slice(0);
const testConcat = () => [].concat(arr);
const testFor = () => {
const result = [];
for (let i = 0; i < arr.length; i++) {
result.push(arr[i]);
}
return result;
};
const testForNew = () => {
const len = arr.length;
const result = new Array(len);
for (let i = 0; i < len; i++) {
result[i] = arr[i];
}
return result;
};
// Run tests
benchmark.do(500000, [
testSlice,
testSlice0,
testConcat,
testFor,
testForNew,
]);