Problem
The current unit value uses Object.create(null, {...}) which creates a prototype-less object. This is unnecessary complexity for a singleton value.
Why It Matters
Object.create(null) is slower than object literals in V8
- The prototype-less nature provides no benefit since reference equality handles the singleton case
- Inconsistent with other frozen singletons (
ok(), err()) which use simple object literals
- Harder to read and understand than
{ [UNIT_BRAND]: true }
Current vs Expected DX
| Aspect |
Current |
Expected |
| Creation |
Object.create(null, { [UNIT_BRAND]: { value: true, ... } }) |
{ [UNIT_BRAND]: true } |
| Readability |
Complex |
Simple |
| V8 Optimization |
Slower |
Faster |
Current Situation
const UNIT_BRAND = Symbol.for("deesse.unit");
export const unit: Unit = Object.create(null, {
[UNIT_BRAND]: { value: true, enumerable: false, writable: false, configurable: false },
}) as Unit;
The Object.create(null, ...) pattern is used to:
- Avoid prototype chain
- Make the brand property non-enumerable
But neither is necessary since we also do reference equality check value === unit.
What Would Change
const UNIT_BRAND = Symbol.for("deesse.unit");
export const unit: Unit = Object.freeze({
[UNIT_BRAND]: true,
}) as Unit;
Simpler, faster, consistent with the rest of the codebase.
Code Example
// Current - unnecessarily complex
export const unit: Unit = Object.create(null, {
[UNIT_BRAND]: { value: true, enumerable: false, writable: false, configurable: false },
}) as Unit;
// Simplified - same behavior, cleaner code
export const unit: Unit = Object.freeze({
[UNIT_BRAND]: true,
}) as Unit;
Co-Authored-By: martyy-code nesalia.inc@gmail.com
Problem
The current
unitvalue usesObject.create(null, {...})which creates a prototype-less object. This is unnecessary complexity for a singleton value.Why It Matters
Object.create(null)is slower than object literals in V8ok(),err()) which use simple object literals{ [UNIT_BRAND]: true }Current vs Expected DX
Object.create(null, { [UNIT_BRAND]: { value: true, ... } }){ [UNIT_BRAND]: true }Current Situation
The
Object.create(null, ...)pattern is used to:But neither is necessary since we also do reference equality check
value === unit.What Would Change
Simpler, faster, consistent with the rest of the codebase.
Code Example
Co-Authored-By: martyy-code nesalia.inc@gmail.com