Skip to content

Commit

Permalink
feat: es bench
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Feb 15, 2023
1 parent be7c12f commit a6ec86b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
12 changes: 12 additions & 0 deletions demo/es-bench/bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {delay} from '@alwatr/util';

import type {MaybePromise} from '@alwatr/type';

export async function bench(name: string, func: () => MaybePromise<void>): Promise<void> {
await delay(1_000);
console.time(name);
for (let i = 100_000; i; i--) {
await func();
}
console.timeEnd(name);
}
81 changes: 81 additions & 0 deletions demo/es-bench/for-in-vs-of.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable camelcase */

import {random} from '@alwatr/math';

import {bench} from './bench.js';

const obj: Record<string, Record<string, string>> = {};
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
*/
16 changes: 16 additions & 0 deletions demo/es-bench/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>ES Bench</title>
<script type="module" src="./for-in-vs-of.js"></script>
<style>
html {
color-scheme: light dark;
}
</style>
</head>
<body>
Waiting...
</body>
</html>
2 changes: 2 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<li><a href="./font/">Font</a></li>
<li><a href="./math/">Math</a></li>
<li><a href="./icon/">Icon</a></li>
<li><a href="./es-bench/">ES Bench</a></li>

</ol>
</body>
</html>
1 change: 1 addition & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a6ec86b

Please sign in to comment.