-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark-worker.js
168 lines (143 loc) · 4.83 KB
/
benchmark-worker.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// The arrays are globally defined because they're shared.
// This is typically not recommended.
let GLOBAL_ARRAY_1 = null;
let GLOBAL_ARRAY_2 = null;
onmessage = (e) => {
let array1 = null;
let array2 = null;
let iteration = null;
let concatCompleted = false;
let pushCompleted = false;
let spreadCompleted = false;
let spread2Completed = false;
/**
* Initializes an array.
*/
function initializeArray(arrayLength, arrayType) {
const results = [];
if (arrayType === 'values') {
for (let i=0; i<arrayLength; i++) {
const value = (i+1);
results.push(value);
}
} else if (arrayType === 'objects') {
for (let i=0; i<arrayLength; i++) {
const result = { p1:(i+1), p2:`Number ${(i+1)}` }; // A generate object with two properties.
results.push(result);
}
}
return results;
}
/**
* Runs a test iteration.
* NOTE: This function assumes array1 and array2 have been initialized.
*/
function runIteration(options) {
iteration = { number: options.number };
array1 = GLOBAL_ARRAY_1;
array2 = GLOBAL_ARRAY_2;
executeConcat(options.useConcat);
executePush(options.usePush);
executeSpread(options.useSpread);
executeSpread2(options.useSpread2);
}
/**
* Merges two arrays using the built-in concat method in JavaScript.
*/
function executeConcat(shouldRun) {
concatCompleted = false;
if (shouldRun) {
try {
const startTime = performance.now();
let result = array1.concat(array2);
iteration.concatRuntime = performance.now() - startTime;
console.log('concat:');
console.log(result);
result = null;
} catch (err) {
iteration.concatRuntime = 'N/A';
}
}
concatCompleted = true;
assertCompleteIteration();
}
/**
* Merges two array using the Array's push method in JavaScript.
*/
function executePush(shouldRun) {
pushCompleted = false;
if (shouldRun) {
try {
let result = array1.slice();
const startTime = performance.now();
for (let i=0; i<array2.length; i++) {
result.push(array2[i]);
}
iteration.pushRuntime = performance.now() - startTime;
console.log('push:');
console.log(result);
result = null;
} catch (err) {
iteration.pushRuntime = 'N/A';
}
}
pushCompleted = true;
assertCompleteIteration();
}
/**
* Merges two arrays using the spread operator.
*/
function executeSpread(shouldRun) {
spreadCompleted = false;
if (shouldRun) {
try {
let result = array1.slice();
const startTime = performance.now();
result.push(...array2);
iteration.spreadRuntime = performance.now() - startTime;
console.log('spread:');
console.log(result);
result = null;
} catch (err) {
iteration.spreadRuntime = 'N/A';
}
}
spreadCompleted = true;
assertCompleteIteration();
}
function executeSpread2(shouldRun) {
spread2Completed = false;
if (shouldRun) {
try {
const startTime = performance.now();
const result = [...array2, ...array1];
console.log('spread 2:');
console.log(result);
iteration.spread2Runtime = performance.now() - startTime;
} catch (err) {
iteration.spreadRuntime = 'N/A';
}
}
spread2Completed = true;
assertCompleteIteration();
}
/**
* Tests to see if the current iteration has completed.
*/
function assertCompleteIteration() {
const isComplete = concatCompleted && pushCompleted && spreadCompleted && spread2Completed;
if (isComplete) {
const response = { command:'test-iteration-completed', iteration:iteration };
postMessage(response);
}
}
// React to the command sent to the user.
if (e.data.command === 'initialize-arrays') {
GLOBAL_ARRAY_1 = initializeArray(e.data.array1Length, e.data.array1Type);
GLOBAL_ARRAY_2 = initializeArray(e.data.array2Length, e.data.array2Type);
const response = { command:'initialize-arrays-completed' };
postMessage(response);
} else if (e.data.command === 'run-iteration') {
runIteration(e.data);
}
};