From a6ec86bc71dba2aec4dc6c4b42a64af75bb2b012 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Mon, 13 Feb 2023 19:18:22 +0330 Subject: [PATCH] feat: es bench --- demo/es-bench/bench.ts | 12 ++++++ demo/es-bench/for-in-vs-of.ts | 81 +++++++++++++++++++++++++++++++++++ demo/es-bench/index.html | 16 +++++++ demo/index.html | 2 + demo/package.json | 1 + 5 files changed, 112 insertions(+) create mode 100644 demo/es-bench/bench.ts create mode 100644 demo/es-bench/for-in-vs-of.ts create mode 100644 demo/es-bench/index.html diff --git a/demo/es-bench/bench.ts b/demo/es-bench/bench.ts new file mode 100644 index 000000000..b87bb566c --- /dev/null +++ b/demo/es-bench/bench.ts @@ -0,0 +1,12 @@ +import {delay} from '@alwatr/util'; + +import type {MaybePromise} from '@alwatr/type'; + +export async function bench(name: string, func: () => MaybePromise): Promise { + await delay(1_000); + console.time(name); + for (let i = 100_000; i; i--) { + await func(); + } + console.timeEnd(name); +} diff --git a/demo/es-bench/for-in-vs-of.ts b/demo/es-bench/for-in-vs-of.ts new file mode 100644 index 000000000..778f94927 --- /dev/null +++ b/demo/es-bench/for-in-vs-of.ts @@ -0,0 +1,81 @@ +/* eslint-disable camelcase */ + +import {random} from '@alwatr/math'; + +import {bench} from './bench.js'; + +const obj: Record> = {}; +let userName = ''; +console.log(userName); + +function prepare(): void { + for (let i = 100; i; i--) { + obj[i] = { + id: 'user_' + i, + fname: random.string(4, 16), + lname: random.string(4, 32), + email: random.string(8, 32), + token: random.string(16), + }; + } +} + +function test_for_in(): void { + for (const key in obj) { + if (!Object.prototype.hasOwnProperty.call(obj, key)) continue; + userName = obj[key].id; + } +} + +function test_for_of_values(): void { + for (const item of Object.values(obj)) { + userName = item.id; + } +} + +function test_for_of_keys(): void { + for (const key of Object.keys(obj)) { + userName = obj[key].id; + } +} + +prepare(); +await bench('for-in', test_for_in); +await bench('for-of-values', test_for_of_values); +await bench('for-of-keys', test_for_of_keys); + +globalThis.document?.body.append(' Done. Check the console.'); + +/* +1000 items, key is numberString (obj[i]) + for-of-values: 1s + for-of-keys: 3s + for-in: 6s + +1000 items, if key is string (obj['user_'+i]) + for-of-keys: 11s + for-in: 15s + for-of-values: 26s + + +100 items, key is numberString (obj[i]) + for-of-values: 139ms + for-of-keys: 342ms + for-in: 599ms + +100 items, if key is string (obj['user_'+i]) + for-of-keys: 651ms + for-in: 960ms + for-of-values: 2159ms + + +10 items, key is numberString (obj[i]) + for-of-values: 54ms + for-of-keys: 70ms + for-in: 107ms + +10 items, if key is string (obj['user_'+i]) + for-in: 28ms + for-of-values: 35ms + for-of-keys: 53ms +*/ diff --git a/demo/es-bench/index.html b/demo/es-bench/index.html new file mode 100644 index 000000000..1e600425e --- /dev/null +++ b/demo/es-bench/index.html @@ -0,0 +1,16 @@ + + + + + ES Bench + + + + + Waiting... + + diff --git a/demo/index.html b/demo/index.html index ed24846c7..754be5368 100644 --- a/demo/index.html +++ b/demo/index.html @@ -20,6 +20,8 @@
  • Font
  • Math
  • Icon
  • +
  • ES Bench
  • + diff --git a/demo/package.json b/demo/package.json index a8457bd16..2e86f3730 100644 --- a/demo/package.json +++ b/demo/package.json @@ -13,6 +13,7 @@ "@alwatr/router": "^0.29.0", "@alwatr/signal": "^0.29.0", "@alwatr/storage-client": "^0.29.0", + "@alwatr/util": "^0.29.0", "@alwatr/storage-engine": "^0.29.0", "@alwatr/token": "^0.29.0", "@alwatr/type": "^0.29.0",