|
1 | 1 | const { startWasiTask } = require("../lib");
|
2 | 2 | const { performance } = require("perf_hooks");
|
3 | 3 |
|
| 4 | +const SAMPLE_ITERATION = 1000000 |
| 5 | + |
4 | 6 | global.benchmarkRunner = function (name, body) {
|
5 | 7 | console.log(`Running '${name}' ...`);
|
6 | 8 | const startTime = performance.now();
|
7 |
| - body(5000); |
| 9 | + body(SAMPLE_ITERATION); |
8 | 10 | const endTime = performance.now();
|
9 | 11 | console.log("done " + (endTime - startTime) + " ms");
|
10 | 12 | };
|
11 | 13 |
|
| 14 | +global.noopFunction = function () {} |
| 15 | +global.jsNumber = 42 |
| 16 | +global.jsString = "myString" |
| 17 | + |
12 | 18 | class JSBenchmark {
|
13 | 19 | constructor(title) {
|
14 | 20 | this.title = title;
|
15 | 21 | }
|
16 | 22 | testSuite(name, body) {
|
17 | 23 | benchmarkRunner(`${this.title}/${name}`, (iteration) => {
|
18 |
| - for (let idx = 0; idx < iteration; idx++) { |
19 |
| - body(); |
20 |
| - } |
| 24 | + body(iteration); |
21 | 25 | });
|
22 | 26 | }
|
23 | 27 | }
|
24 | 28 |
|
25 | 29 | const serialization = new JSBenchmark("Serialization");
|
26 |
| -serialization.testSuite("Write JavaScript number directly", () => { |
| 30 | +serialization.testSuite("Call JavaScript function directly", (n) => { |
| 31 | + for (let idx = 0; idx < n; idx++) { |
| 32 | + global.noopFunction() |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +serialization.testSuite("Assign JavaScript number directly", (n) => { |
27 | 37 | const jsNumber = 42;
|
28 | 38 | const object = global;
|
29 |
| - for (let idx = 0; idx < 100; idx++) { |
30 |
| - object["numberValue" + idx] = jsNumber; |
| 39 | + const key = "numberValue" |
| 40 | + for (let idx = 0; idx < n; idx++) { |
| 41 | + object[key] = jsNumber; |
31 | 42 | }
|
32 | 43 | });
|
33 | 44 |
|
34 |
| -serialization.testSuite("Write JavaScript string directly", () => { |
| 45 | +serialization.testSuite("Call with JavaScript number directly", (n) => { |
| 46 | + const jsNumber = 42; |
| 47 | + for (let idx = 0; idx < n; idx++) { |
| 48 | + global.noopFunction(jsNumber) |
| 49 | + } |
| 50 | +}); |
| 51 | + |
| 52 | +serialization.testSuite("Write JavaScript string directly", (n) => { |
35 | 53 | const jsString = "Hello, world";
|
36 | 54 | const object = global;
|
37 |
| - for (let idx = 0; idx < 100; idx++) { |
38 |
| - object["stringValue" + idx] = jsString; |
| 55 | + const key = "stringValue" |
| 56 | + for (let idx = 0; idx < n; idx++) { |
| 57 | + object[key] = jsString; |
| 58 | + } |
| 59 | +}); |
| 60 | + |
| 61 | +serialization.testSuite("Call with JavaScript string directly", (n) => { |
| 62 | + const jsString = "Hello, world"; |
| 63 | + for (let idx = 0; idx < n; idx++) { |
| 64 | + global.noopFunction(jsString) |
39 | 65 | }
|
40 | 66 | });
|
41 | 67 |
|
|
0 commit comments