Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Cache global vs reference
(version: 1)
Comparing performance of:
use global vs cache global
Created:
9 years ago
by:
Registered User
Jump to the latest result
Tests:
use global
var value = 0; (function() { function run() { for (var i = 0; i < 10000; i++) { value = Math.min(value, Math.random()); } } run(); })();
cache global
var value = 0; (function() { var MathGlobal = Math; function run() { for (var i = 0; i < 10000; i++) { value = MathGlobal.min(value, MathGlobal.random()); } } run(); })();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
use global
cache global
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
use global
10256.3 Ops/sec
cache global
10423.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and its options, pros, and cons. **Benchmark Overview** The test measures the performance difference between two approaches: using a global variable (`var value = 0;`) and caching the `Math` object in a local scope (`var MathGlobal = Math;`). The goal is to determine which approach has better cache locality, leading to improved performance. **Options Compared** There are two options compared: 1. **Use Global**: In this approach, a global variable `value` is declared, and then the `Math.min()` function is called with the global `value` as an argument. 2. **Cache Global**: In this approach, a local scope creates a variable `MathGlobal` that refers to the `Math` object, which is then used in the `Math.min()` function. **Pros and Cons** * **Use Global (Option 1)** * Pros: * Simpler code * No need to create a new scope or context for the `Math` object. * Cons: * Poor cache locality, as the global variable is not optimized for caching in JavaScript engines. * **Cache Global (Option 2)** * Pros: * Better cache locality, as the local `MathGlobal` scope is more likely to be cached by the JavaScript engine. * More efficient performance * Cons: * Requires creating a new scope or context for the `Math` object. * Code may appear more complex **Library and Special JS Feature** In both test cases, no libraries are used. However, they do utilize a special JavaScript feature: * **Lexical Scoping**: The test uses lexical scoping to create local variables (`value`, `MathGlobal`) within the scope of each benchmark function. **Other Considerations** The test assumes that the JavaScript engine being tested (e.g., V8 in Chrome) will cache the execution context for the local variable `value` or `MathGlobal`. This assumption is based on the behavior of modern JavaScript engines, which use a technique called **register-based caching** to optimize performance. If not using these approaches, you can implement additional optimization techniques such as: * **Using native functions**: Consider replacing the global object calls with native functions that have optimized implementations. * **Minimizing scope creation**: Reduce the number of scopes created by reusing variables or using closures to share state between functions. * **Loop unrolling and optimizations**: Optimize loops for better performance, such as loop unrolling or using SIMD instructions (if supported). Keep in mind that the effectiveness of these techniques depends on the specific JavaScript engine being used. **Alternatives** For measuring JavaScript performance and benchmarking: 1. **Benchmark.js**: A popular tool for writing fast and accurate benchmarks. 2. **V8 Benchmark Suite**: A collection of benchmark suites designed to test the performance of the V8 JavaScript engine. 3. **jsPerf**: A web-based testing framework for comparing browser performances. Each alternative has its strengths and weaknesses, so choose the one that best suits your specific use case and requirements.
Related benchmarks:
cache vs hard
cache vs hard
Cached Getter (class) vs Getter vs Proxy
for-loop cached variables / access caching + V8 hints
Object.values vs cached array
Comments
Confirm delete:
Do you really want to delete benchmark?