-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathwebidl-convert-to-int.js
More file actions
82 lines (75 loc) Β· 1.89 KB
/
webidl-convert-to-int.js
File metadata and controls
82 lines (75 loc) Β· 1.89 KB
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
'use strict';
const assert = require('assert');
const common = require('../common.js');
const bench = common.createBenchmark(main, {
converter: [
'byte',
'octet',
'unsigned short',
'unsigned long',
'long long',
],
input: [
'integer',
'fractional',
'wrap',
'clamp',
'enforce-range',
'object',
],
n: [1e6],
}, { flags: ['--expose-internals'] });
function getConverter(converter) {
switch (converter) {
case 'byte':
return { bitLength: 8, signedness: 'signed' };
case 'octet':
return { bitLength: 8 };
case 'unsigned short':
return { bitLength: 16 };
case 'unsigned long':
return { bitLength: 32 };
case 'long long':
return { bitLength: 64, signedness: 'signed' };
default:
throw new Error(`Unsupported converter: ${converter}`);
}
}
function getInput(input) {
switch (input) {
case 'integer':
return { value: 7 };
case 'fractional':
return { value: 7.9 };
case 'wrap':
return { value: 2 ** 63 + 2 ** 11 };
case 'clamp':
return { value: 300.8, options: { clamp: true } };
case 'enforce-range':
return { value: 7.9, options: { enforceRange: true } };
case 'object':
return {
value: {
valueOf() { return 7; },
},
};
default:
throw new Error(`Unsupported input: ${input}`);
}
}
function main({ n, converter, input }) {
const { convertToInt } = require('internal/webidl');
const { bitLength, signedness } = getConverter(converter);
const { value, options } = getInput(input);
let noDead;
bench.start();
if (options === undefined) {
for (let i = 0; i < n; i++)
noDead = convertToInt(value, bitLength, signedness);
} else {
for (let i = 0; i < n; i++)
noDead = convertToInt(value, bitLength, signedness, options);
}
bench.end(n);
assert.strictEqual(typeof noDead, 'number');
}