Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Access Object Property vs Global Let vs Private Let
(version: 0)
Comparing performance of:
Object vs Global Let vs Private Let
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const array = Array(1000); for (let arr of array) arr = Math.random(10); const obj = { a: 0, b: 0, } var ofunc = () => { for (let arr of array) { obj.a += arr; obj.b += obj.a + arr; } } let ga = 0; let gb = 0; var gfunc = () => { for (let arr of array) { ga += arr; gb += ga + arr; } } var pfunc = (() => { let a = 0; let b = 0; return () => { for (let arr of array) { a += arr; b += a + arr; } } })();
Tests:
Object
ofunc();
Global Let
gfunc();
Private Let
pfunc();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object
Global Let
Private Let
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.2
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object
62787.1 Ops/sec
Global Let
60405.1 Ops/sec
Private Let
60598.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark compares three different approaches to accessing an object's property: 1. **Direct Access**: `obj.a += arr;` and `obj.b += obj.a + arr;` 2. **Global Variable**: `ga += arr;` and `gb += ga + arr;` 3. **Private Variable** (using Immediately Invoked Function Expression, IIFE): `a += arr;` and `b += a + arr;` The benchmark measures the execution time of each approach. **Approaches Compared** 1. **Direct Access**: This approach accesses the object's property directly using dot notation (`obj.a`) or bracket notation (`obj.b`). This is the most straightforward way to access an object's property. 2. **Global Variable**: In this approach, two global variables `ga` and `gb` are used to store the cumulative values. The `gfunc` function updates these global variables in each iteration of the loop. This approach uses a global variable, which can lead to naming conflicts or unexpected behavior if not managed carefully. 3. **Private Variable** (using IIFE): In this approach, an IIFE is used to create a private scope for the variables `a` and `b`. The `pfunc` function returns another function that updates these private variables in each iteration of the loop. This approach uses a private variable, which can help encapsulate data and avoid global variable pollution. **Pros and Cons of Each Approach** 1. **Direct Access** * Pros: straightforward, easy to understand * Cons: may not be optimal due to object property lookup overhead 2. **Global Variable** * Pros: simple, easy to implement * Cons: uses a global variable, which can lead to naming conflicts or unexpected behavior 3. **Private Variable** (using IIFE) * Pros: encapsulates data, avoids global variable pollution * Cons: more complex setup, may require additional setup for the IIFE **Library and Special JavaScript Features** In this benchmark, no libraries are used, but some special JavaScript features are employed: 1. **Immediately Invoked Function Expression (IIFE)**: An IIFE is used to create a private scope for the variables `a` and `b`. This feature helps encapsulate data and avoid global variable pollution. 2. **Arrow Functions**: The `pfunc` function uses an arrow function, which is a concise way to define small functions without declaring them with `function`. **Other Alternatives** If you need to optimize object property access, consider the following alternatives: 1. **Using a cache**: If the object's properties are not changing frequently, using a cache to store the results of previous accesses can help reduce overhead. 2. **Using a specialized library**: Libraries like Lodash or Ramda provide optimized functions for common data manipulation tasks, including object property access. 3. **Compiling to native code**: Compilers like V8 (used by Chrome) or SpiderMonkey (used by Firefox) can optimize JavaScript code for performance, but this requires significant expertise and resources. Keep in mind that the best approach depends on your specific use case, data characteristics, and performance requirements.
Related benchmarks:
Lodash.js vs Native Remove Duplicates
Passing new objects with raw values vs passing just raw values vs array of values
Array push or set
array vs set lookup
Comments
Confirm delete:
Do you really want to delete benchmark?