generated from AlexXanderGrib/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
860b5f4
commit d6a3a41
Showing
35 changed files
with
2,246 additions
and
872 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 |
---|---|---|
@@ -1,28 +1,6 @@ | ||
import process from "node:process"; | ||
import { benchmark } from "./common.js"; | ||
|
||
const none = { _type: "none" }; | ||
const just = (value) => ({ _type: "just", value }); | ||
|
||
const store = Array(10_000); | ||
const base = process.memoryUsage().heapUsed; | ||
let allocated = 0; | ||
|
||
for (let i = 0; i < store.length; i++) { | ||
const random = Math.random(); | ||
|
||
store[i] = random > 0.5 ? just(random) : none; | ||
allocated += store[i] !== none; | ||
} | ||
|
||
const diff = process.memoryUsage().heapUsed - base; | ||
const KB = 1024; | ||
|
||
const total = diff; | ||
const perJust = total / allocated; | ||
|
||
console.log( | ||
(total / KB).toFixed(2), | ||
"KB -", | ||
Math.ceil(perJust), | ||
"bytes / Just" | ||
); | ||
benchmark({ name: "object", just, none }); |
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 |
---|---|---|
@@ -1,28 +1,6 @@ | ||
import process from "node:process"; | ||
import { benchmark } from "./common.js"; | ||
|
||
const none = Object.freeze({ _type: "none" }); | ||
const just = (value) => Object.freeze({ _type: "just", value }); | ||
|
||
const store = Array(10_000); | ||
const base = process.memoryUsage().heapUsed; | ||
|
||
let allocated = 0; | ||
|
||
for (let i = 0; i < store.length; i++) { | ||
const random = Math.random(); | ||
|
||
store[i] = random > 0.5 ? just(random) : none; | ||
allocated += store[i] !== none; | ||
} | ||
|
||
const diff = process.memoryUsage().heapUsed - base; | ||
const KB = 1024; | ||
const total = diff; | ||
const perJust = total / allocated; | ||
|
||
console.log( | ||
(total / KB).toFixed(2), | ||
"KB -", | ||
Math.ceil(perJust), | ||
"bytes / Just" | ||
); | ||
benchmark({ name: "frozen", just, none }); |
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
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,38 @@ | ||
import { benchmark } from "./common.js"; | ||
|
||
const Type = Object.freeze({ | ||
None: 0, | ||
Just: 1 | ||
}); | ||
|
||
class Just { | ||
static create(value) { | ||
return new Just(value); | ||
} | ||
|
||
get type() { | ||
return Type.Just; | ||
} | ||
|
||
constructor(value) { | ||
this.value = value; | ||
Object.freeze(this); | ||
} | ||
} | ||
|
||
class None { | ||
static instance = new None(); | ||
static create() { | ||
return None.instance; | ||
} | ||
|
||
get type() { | ||
return Type.None; | ||
} | ||
|
||
constructor() { | ||
Object.freeze(this); | ||
} | ||
} | ||
|
||
benchmark({ name: "enum + getter", just: Just.create, none: None.instance }) |
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
import { benchmark } from "./common.js"; | ||
|
||
const Type = Object.freeze({ | ||
None: "None", | ||
Just: "Just" | ||
}); | ||
|
||
class Just { | ||
static create(value) { | ||
return new Just(value); | ||
} | ||
|
||
get type() { | ||
return Type.Just; | ||
} | ||
|
||
constructor(value) { | ||
this.value = value; | ||
Object.freeze(this); | ||
} | ||
} | ||
|
||
class None { | ||
static instance = new None(); | ||
static create() { | ||
return None.instance; | ||
} | ||
|
||
get type() { | ||
return Type.None; | ||
} | ||
|
||
constructor() { | ||
Object.freeze(this); | ||
} | ||
} | ||
|
||
benchmark({ | ||
name: "full type name + getter", | ||
just: Just.create, | ||
none: None.instance | ||
}); |
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,43 @@ | ||
export function benchmark({ | ||
name, | ||
just, | ||
none, | ||
count = 100_000, | ||
iterations = 20 | ||
}) { | ||
console.log("Benchmarking implementation:", { name, count, iterations }); | ||
const results = []; | ||
|
||
for (let i = 0; i < iterations; i++) { | ||
const store = Array(count); | ||
const base = process.memoryUsage().heapUsed; | ||
let allocated = 0; | ||
|
||
for (let i = 0; i < store.length; i++) { | ||
const random = Math.random(); | ||
const isJust = random > 0.5; | ||
|
||
store[i] = random > 0.5 ? just(random) : none; | ||
allocated += isJust; | ||
} | ||
|
||
const diff = process.memoryUsage().heapUsed - base; | ||
const KB = 1024; | ||
|
||
const total = diff; | ||
const perJust = total / allocated; | ||
|
||
results.push(perJust); | ||
|
||
console.log( | ||
(total / KB).toFixed(2), | ||
"KB diff -", | ||
Math.ceil(perJust), | ||
"bytes / Just" | ||
); | ||
} | ||
|
||
const average = results.reduce((a, b) => a + b, 0) / results.length; | ||
|
||
console.log("Benchmark done", { averageBytes: average }); | ||
} |
Oops, something went wrong.