Skip to content

Commit

Permalink
benchmark: added new benchmarks for blob
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
H4ad authored and debadree25 committed Apr 15, 2024
1 parent 65128b7 commit bc25ec1
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
22 changes: 22 additions & 0 deletions benchmark/blob/clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const common = require('../common.js');
const {
Blob,
} = require('node:buffer');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [50e3],
});

let _cloneResult;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
_cloneResult = structuredClone(new Blob(['hello']));
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_cloneResult);
}
48 changes: 48 additions & 0 deletions benchmark/blob/creation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
const common = require('../common.js');
const {
Blob,
} = require('node:buffer');
const assert = require('assert');

const options = {
flags: ['--expose-internals'],
};

const bench = common.createBenchmark(main, {
n: [50e3],
kind: [
'Blob',
'createBlob',
],
}, options);

let _blob;
let _createBlob;

function main({ n, kind }) {
switch (kind) {
case 'Blob': {
bench.start();
for (let i = 0; i < n; ++i)
_blob = new Blob(['hello']);
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_blob);
break;
} case 'createBlob': {
const { createBlob } = require('internal/blob');
const reusableBlob = new Blob(['hello']);
bench.start();
for (let i = 0; i < n; ++i)
_createBlob = createBlob(reusableBlob, reusableBlob.size);
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_createBlob);
break;
} default:
throw new Error('Invalid kind');
}
}
22 changes: 22 additions & 0 deletions benchmark/blob/resolveObjectURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const common = require('../common.js');
const { Blob, resolveObjectURL } = require('node:buffer');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [50e3],
});

let _resolveObjectURL;

function main({ n }) {
const blob = new Blob(['hello']);
const id = URL.createObjectURL(blob);
bench.start();
for (let i = 0; i < n; ++i)
_resolveObjectURL = resolveObjectURL(id);
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_resolveObjectURL);
}
27 changes: 27 additions & 0 deletions benchmark/blob/slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common.js');
const { Blob } = require('node:buffer');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [50e3],
dataSize: [
5,
30 * 1024,
],
});

let _sliceResult;

function main({ n, dataSize }) {
const data = 'a'.repeat(dataSize);
const reusableBlob = new Blob([data]);

bench.start();
for (let i = 0; i < n; ++i)
_sliceResult = reusableBlob.slice(0, Math.floor(data.length / 2));
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_sliceResult);
}

0 comments on commit bc25ec1

Please sign in to comment.