Skip to content

Commit 86fe5a8

Browse files
H4adnodejs-github-bot
authored andcommitted
benchmark: added new benchmarks for blob
PR-URL: nodejs#49730 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Zeyu "Alex" Yang <[email protected]> Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent 328bdac commit 86fe5a8

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

benchmark/blob/clone.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const {
4+
Blob,
5+
} = require('node:buffer');
6+
const assert = require('assert');
7+
8+
const bench = common.createBenchmark(main, {
9+
n: [50e3],
10+
});
11+
12+
let _cloneResult;
13+
14+
function main({ n }) {
15+
bench.start();
16+
for (let i = 0; i < n; ++i)
17+
_cloneResult = structuredClone(new Blob(['hello']));
18+
bench.end(n);
19+
20+
// Avoid V8 deadcode (elimination)
21+
assert.ok(_cloneResult);
22+
}

benchmark/blob/creation.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const {
4+
Blob,
5+
} = require('node:buffer');
6+
const assert = require('assert');
7+
8+
const options = {
9+
flags: ['--expose-internals'],
10+
};
11+
12+
const bench = common.createBenchmark(main, {
13+
n: [50e3],
14+
kind: [
15+
'Blob',
16+
'createBlob',
17+
],
18+
}, options);
19+
20+
let _blob;
21+
let _createBlob;
22+
23+
function main({ n, kind }) {
24+
switch (kind) {
25+
case 'Blob': {
26+
bench.start();
27+
for (let i = 0; i < n; ++i)
28+
_blob = new Blob(['hello']);
29+
bench.end(n);
30+
31+
// Avoid V8 deadcode (elimination)
32+
assert.ok(_blob);
33+
break;
34+
} case 'createBlob': {
35+
const { createBlob } = require('internal/blob');
36+
const reusableBlob = new Blob(['hello']);
37+
bench.start();
38+
for (let i = 0; i < n; ++i)
39+
_createBlob = createBlob(reusableBlob, reusableBlob.size);
40+
bench.end(n);
41+
42+
// Avoid V8 deadcode (elimination)
43+
assert.ok(_createBlob);
44+
break;
45+
} default:
46+
throw new Error('Invalid kind');
47+
}
48+
}

benchmark/blob/resolveObjectURL.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const { Blob, resolveObjectURL } = require('node:buffer');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [50e3],
8+
});
9+
10+
let _resolveObjectURL;
11+
12+
function main({ n }) {
13+
const blob = new Blob(['hello']);
14+
const id = URL.createObjectURL(blob);
15+
bench.start();
16+
for (let i = 0; i < n; ++i)
17+
_resolveObjectURL = resolveObjectURL(id);
18+
bench.end(n);
19+
20+
// Avoid V8 deadcode (elimination)
21+
assert.ok(_resolveObjectURL);
22+
}

benchmark/blob/slice.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const { Blob } = require('node:buffer');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [50e3],
8+
dataSize: [
9+
5,
10+
30 * 1024,
11+
],
12+
});
13+
14+
let _sliceResult;
15+
16+
function main({ n, dataSize }) {
17+
const data = 'a'.repeat(dataSize);
18+
const reusableBlob = new Blob([data]);
19+
20+
bench.start();
21+
for (let i = 0; i < n; ++i)
22+
_sliceResult = reusableBlob.slice(0, Math.floor(data.length / 2));
23+
bench.end(n);
24+
25+
// Avoid V8 deadcode (elimination)
26+
assert.ok(_sliceResult);
27+
}

0 commit comments

Comments
 (0)