-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
optimization-test.js
43 lines (39 loc) · 1018 Bytes
/
optimization-test.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
/* eslint-disable no-fallthrough */
import process from 'node:process';
import assert from 'node:assert';
import v8 from 'v8-natives';
import pify from './index.js';
function assertOptimized(fn, name) {
const status = v8.getOptimizationStatus(fn);
switch (status) {
case 1:
// `fn` is optimized
console.log('pify is optimized');
return;
case 2:
assert(false, `${name} is not optimized (${status})`);
case 3:
// `fn` is always optimized
return;
case 4:
assert(false, `${name} is never optimized (${status})`);
case 6:
assert(false, `${name} is maybe deoptimized (${status})`);
default:
assert(false, `unknown OptimizationStatus: ${status} (${name})`);
}
}
const fn = pify({
unicorn(callback) {
callback(null, 'unicorn');
},
});
try {
await fn.unicorn();
v8.optimizeFunctionOnNextCall(fn.unicorn);
await fn.unicorn();
assertOptimized(fn.unicorn, 'unicorn');
} catch (error) {
console.error(error);
process.exit(1); // eslint-disable-line unicorn/no-process-exit
}