From 96ec7eede945e63b3ce8bd20834f075979bd78a0 Mon Sep 17 00:00:00 2001 From: jakecastelli <38635403+jakecastelli@users.noreply.github.com> Date: Mon, 23 Sep 2024 23:16:14 +0930 Subject: [PATCH] benchmark: add webstorage benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/55040 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Matteo Collina Reviewed-By: Rafael Gonzaga --- benchmark/webstorage/getItem.js | 75 ++++++++++++++++++++++++++++++ benchmark/webstorage/removeItem.js | 75 ++++++++++++++++++++++++++++++ benchmark/webstorage/setItem.js | 63 +++++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 benchmark/webstorage/getItem.js create mode 100644 benchmark/webstorage/removeItem.js create mode 100644 benchmark/webstorage/setItem.js diff --git a/benchmark/webstorage/getItem.js b/benchmark/webstorage/getItem.js new file mode 100644 index 00000000000..c383f91ee0d --- /dev/null +++ b/benchmark/webstorage/getItem.js @@ -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'); + } +} diff --git a/benchmark/webstorage/removeItem.js b/benchmark/webstorage/removeItem.js new file mode 100644 index 00000000000..330f17bdf35 --- /dev/null +++ b/benchmark/webstorage/removeItem.js @@ -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-removeItem', + 'localStorage-delete', + 'sessionStorage-removeItem', + 'sessionStorage-delete', + ], + // 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-removeItem': + fillStorage(localStorage, n); + bench.start(); + for (let i = 0; i < n; i++) { + localStorage.removeItem(i); + } + bench.end(n); + break; + case 'localStorage-delete': + fillStorage(localStorage, n); + bench.start(); + for (let i = 0; i < n; i++) { + delete localStorage[i]; + } + bench.end(n); + break; + case 'sessionStorage-removeItem': + fillStorage(sessionStorage, n); + bench.start(); + for (let i = 0; i < n; i++) { + sessionStorage.removeItem(i); + } + bench.end(n); + break; + + case 'sessionStorage-delete': + fillStorage(localStorage, n); + bench.start(); + for (let i = 0; i < n; i++) { + delete sessionStorage[i]; + } + bench.end(n); + break; + default: + new Error('Invalid type'); + } +} diff --git a/benchmark/webstorage/setItem.js b/benchmark/webstorage/setItem.js new file mode 100644 index 00000000000..e6ef2f866de --- /dev/null +++ b/benchmark/webstorage/setItem.js @@ -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'); + } +}