Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
js private field
(version: 0)
Comparing performance of:
private vs public
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
private
class MyClass { #_propertyOne = 1; #_propertyTwo = 2; #_sum = 0; sumProperties() { for (let i = 0; i < 10_000; i++) this.#_sum += this.#_propertyOne + this.#_propertyTwo; } } new MyClass().sumProperties();
public
class MyClass { _propertyOne = 1; _propertyTwo = 2; _sum = 0; sumProperties() { for (let i = 0; i < 10_000; i++) this._sum += this._propertyOne + this._propertyTwo; } } new MyClass().sumProperties();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
private
public
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
private
4759.4 Ops/sec
public
11184.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Understanding the Benchmark** The provided benchmark measures the performance difference between two approaches to declaring properties in JavaScript classes: private and public. In JavaScript, there is no strict enforcement of property access modifiers like `public` or `private`. Instead, developers often use conventional naming conventions (e.g., `m_` for private properties and `_` for public properties) to indicate the intended accessibility of a property. However, this convention is not enforced by the language itself. **Private Field Approach** The first test case uses a pseudo-private field approach, where the `_propertyOne`, `_propertyTwo`, and `_sum` fields are prefixed with an underscore (`_`) to signal that they should be treated as private. This approach relies on developer discipline and convention rather than strict enforcement by the JavaScript engine. **Public Field Approach** The second test case uses a pseudo-public field approach, where the `_propertyOne`, `_propertyTwo`, and `_sum` fields are not prefixed with an underscore. This approach is often used in older JavaScript versions or libraries that do not support modern JavaScript features like private fields (introduced in ECMAScript 2020). **Comparison of Approaches** When comparing these two approaches, we can consider the following pros and cons: * **Private Field Approach (with `_`)** + Pros: - Follows modern JavaScript convention - Can help prevent accidental or intentional property exposure + Cons: - Requires developer discipline and adherence to naming conventions - May not be compatible with older JavaScript versions or libraries that do not support private fields * **Public Field Approach (without `_`)** + Pros: - More widely supported across different JavaScript versions and libraries + Cons: - Lacks explicit indication of property accessibility, making it easier for developers to accidentally expose properties **Library: `let` vs. `const`** There is no library involved in this benchmark. However, if you're interested in exploring alternatives, consider using the `let` keyword instead of `const`. This can provide similar benefits to private fields by scoping variables and preventing accidental reassignment. **Special JavaScript Feature: Rest Operator (Destructuring)** There is no explicit mention of special JavaScript features like the rest operator (`...`) or destructuring in the provided benchmark. However, if you're interested in exploring these features, consider using them in your tests to further evaluate performance benefits. **Alternative Benchmarks** Other alternatives to measure performance differences in JavaScript include: * Using different variable scoping techniques (e.g., `var` vs. `let` vs. `const`) * Evaluating the impact of different data structures and algorithms on performance * Comparing the performance of modern JavaScript features like async/await, generators, or classes against older approaches Keep in mind that the specific characteristics and requirements of your benchmark will dictate which alternatives are most suitable for evaluation.
Related benchmarks:
Object values vs _.values vs improved polyfill
undefined check tests
Private vs public class field access
Test_apr2522
Check if Object has Length in JavaScript
Comments
Confirm delete:
Do you really want to delete benchmark?