-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add es benchmarks tests (#822)
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters