Skip to content

Commit

Permalink
benchmark: add webstorage benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecastelli committed Sep 21, 2024
1 parent 2a1607c commit fba0609
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
75 changes: 75 additions & 0 deletions benchmark/webstorage/getItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';
const { join } = require('node:path');
const common = require('../common.js');
const tmpdir = require('../../test/common/tmpdir');

let cnt = 0;

tmpdir.refresh();

function nextLocalStorage() {
return join(tmpdir.path, `${++cnt}.localstorage`);
}

const options = {
flags: ['--experimental-webstorage', `--localstorage-file=${nextLocalStorage()}`],
};

const bench = common.createBenchmark(main, {
type: ['localStorage-getItem',
'localStorage-getter',
'sessionStorage-getItem',
'sessionStorage-getter'],
// Note: web storage has only 10mb quota
n: [1e5],
}, options);

function fillStorage(storage, n) {
for (let i = 0; i < n; i++) {
storage.setItem(i, i);
}
}

function main({ n, type }) {
const localStorage = globalThis.localStorage;
const sessionStorage = globalThis.sessionStorage;

switch (type) {
case 'localStorage-getItem':
fillStorage(localStorage, n);
bench.start();
for (let i = 0; i < n; i++) {
localStorage.getItem(i);
}
bench.end(n);
break;
case 'localStorage-getter':
fillStorage(localStorage, n);
bench.start();
for (let i = 0; i < n; i++) {
// eslint-disable-next-line no-unused-expressions
localStorage[i];
}
bench.end(n);
break;
case 'sessionStorage-getItem':
fillStorage(sessionStorage, n);
bench.start();
for (let i = 0; i < n; i++) {
sessionStorage.getItem(i);
}
bench.end(n);
break;
case 'sessionStorage-getter':
fillStorage(sessionStorage, n);
bench.start();
for (let i = 0; i < n; i++) {
// eslint-disable-next-line no-unused-expressions
sessionStorage[i];
}
bench.end(n);
break;
default:
new Error('Invalid type');
}
}
55 changes: 55 additions & 0 deletions benchmark/webstorage/removeItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
const { join } = require('node:path');
const common = require('../common.js');
const tmpdir = require('../../test/common/tmpdir');

let cnt = 0;

tmpdir.refresh();

function nextLocalStorage() {
return join(tmpdir.path, `${++cnt}.localstorage`);
}

const options = {
flags: ['--experimental-webstorage', `--localstorage-file=${nextLocalStorage()}`],
};

const bench = common.createBenchmark(main, {
type: ['localStorage',
'sessionStorage'],
// Note: web storage has only 10mb quota
n: [1e5],
}, options);

function fillStorage(storage, n) {
for (let i = 0; i < n; i++) {
storage.setItem(i, i);
}
}

function main({ n, type }) {
const localStorage = globalThis.localStorage;
const sessionStorage = globalThis.sessionStorage;

switch (type) {
case 'localStorage':
fillStorage(localStorage, n);
bench.start();
for (let i = 0; i < n; i++) {
localStorage.removeItem(i);
}
bench.end(n);
break;
case 'sessionStorage':
fillStorage(sessionStorage, n);
bench.start();
for (let i = 0; i < n; i++) {
sessionStorage.removeItem(i);
}
bench.end(n);
break;
default:
new Error('Invalid type');
}
}
63 changes: 63 additions & 0 deletions benchmark/webstorage/setItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';
const { join } = require('node:path');
const common = require('../common.js');
const tmpdir = require('../../test/common/tmpdir');

let cnt = 0;

tmpdir.refresh();

function nextLocalStorage() {
return join(tmpdir.path, `${++cnt}.localstorage`);
}

const options = {
flags: ['--experimental-webstorage', `--localstorage-file=${nextLocalStorage()}`],
};

const bench = common.createBenchmark(main, {
type: ['localStorage-setItem',
'localStorage-setter',
'sessionStorage-setItem',
'sessionStorage-setter'],
// Note: web storage has only 10mb quota
n: [1e5],
}, options);

function main({ n, type }) {
const localStorage = globalThis.localStorage;
const sessionStorage = globalThis.sessionStorage;

switch (type) {
case 'localStorage-setItem':
bench.start();
for (let i = 0; i < n; i++) {
localStorage.setItem(i, i);
}
bench.end(n);
break;
case 'localStorage-setter':
bench.start();
for (let i = 0; i < n; i++) {
localStorage[i] = i;
}
bench.end(n);
break;
case 'sessionStorage-setItem':
bench.start();
for (let i = 0; i < n; i++) {
sessionStorage.setItem(i, i);
}
bench.end(n);
break;
case 'sessionStorage-setter':
bench.start();
for (let i = 0; i < n; i++) {
sessionStorage[i] = i;
}
bench.end(n);
break;
default:
new Error('Invalid type');
}
}

0 comments on commit fba0609

Please sign in to comment.