Skip to content

[refactor]: simplify unit creation - remove Object.create(null) #312

@codewizdave

Description

@codewizdave

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:

  1. Avoid prototype chain
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions