Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
private properties in weakmap vs symbol vs # class syntax
(version: 3)
Comparing performance of:
Class vs WeakMap vs Symbols
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
class Person { static #counter = 0 #age constructor(name, age) { this.name = name; this.#age = age; } showPublic() { return this.name; } showPrivate() { return this.#age; } } window.__person = new Person("j", 22) // WeakMap const privKs = new WeakMap(); const proto = { showPrivate() { return privKs.get(this).age }, showPublic() { return this.name } } function User(name, age) { const obj = {name, age} privKs.set(obj, {age}) return Object.setPrototypeOf(obj, proto) } window.__user = User("j", 11) // Symbols const priv_ = Symbol("private data") const proto2 = { showPrivate() { return this[priv_].age }, showPublic() { return this.name } } function Teacher(name, age) { const obj = {name, [priv_]: {age}} return Object.setPrototypeOf(obj, proto2) } window.__teacher = Teacher("j", 22)
Tests:
Class
window.__person.showPrivate()
WeakMap
window.__user.showPrivate()
Symbols
window.__teacher.showPrivate()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Class
WeakMap
Symbols
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0
Browser/OS:
Firefox 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Class
5153154.5 Ops/sec
WeakMap
4626445.5 Ops/sec
Symbols
5303464.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and explain what is being tested, compared, and their pros and cons. **Benchmark Definition:** The benchmark measures the performance of three different approaches to store private data in JavaScript: 1. **Class syntax**: Using a class with a private property (`#age`) and getter methods for public and private access. 2. **WeakMap**: Using a WeakMap to store an object with private data, where the key is another object that contains the private data. 3. **Symbols**: Using a symbol as a private property key, similar to a WeakMap. **Options Compared:** * Class syntax (using `#` notation for private properties) * WeakMap * Symbols **Pros and Cons of Each Approach:** 1. **Class syntax**: * Pros: + Easy to understand and implement + Provides strong typing and type checking at runtime * Cons: + May lead to over-engineering or unnecessary complexity + Can be slower due to the overhead of getter methods 2. **WeakMap**: * Pros: + Fast and lightweight + Flexible, as it allows for custom key objects * Cons: + May require more manual memory management (e.g., garbage collection) + Can be less intuitive to use, especially for those without prior experience with WeakMaps 3. **Symbols**: * Pros: + Fast and lightweight + Similar syntax to object property access (i.e., `obj[priv_]`) * Cons: + May lead to naming conflicts if not used carefully + Can be less readable or maintainable, especially in complex codebases **Library Usage:** The benchmark uses the following libraries: 1. **`WeakMap`**: A built-in JavaScript object that stores mappings of values to keys using weak references. 2. **`Symbol`**: A built-in JavaScript primitive used to create unique symbols. **Special JS Feature/Syntax:** None mentioned in this explanation. **Other Alternatives:** If you need alternative approaches, consider the following: 1. **Object.freeze() and getter methods**: Use `Object.freeze()` to create an immutable object with private properties, accessed via getter methods. 2. **Proxy objects**: Create proxy objects using the `Proxy` constructor to control access to private data. 3. **Encapsulate with a closure**: Encapsulate data within a closure to limit its scope and accessibility. Keep in mind that these alternatives may have trade-offs in terms of performance, readability, or maintainability, depending on your specific use case. **Benchmark Preparation Code Explanation:** The provided script creates three classes (`Person`, `User`, and `Teacher`) with different approaches to store private data: * Class syntax: uses the `#` notation for private properties * WeakMap: stores an object with private data using a WeakMap * Symbols: uses a symbol as a private property key Each class has getter methods for public and private access. The script also defines a `proto` and `proto2` objects, which serve as the prototype for each class. The `User` and `Teacher` constructors use these prototypes to create new instances with the specified private data. In summary, this benchmark compares three approaches to store private data in JavaScript: Class syntax using getter methods, WeakMap, and Symbols.
Related benchmarks:
WeakMap vs "Symbol with WeakMap fallback"
Getter / Setter vs Proxy vs Events___
WeakMap vs "Symbol with WeakMap fallback" v2
WeakMap vs Symbol Property vs String Property
Comments
Confirm delete:
Do you really want to delete benchmark?