Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Variable Performance (const vs let vs var)
(version: 1)
Comparing performance of:
TEST_1: VAR vs TEST_2: LET vs TEST_3: CONST
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
TEST_1: VAR
var g = { e: [] } g.o = function(x) { g.e.push(...[1,2,3]) } g.o()
TEST_2: LET
let g = { e: [] } g.o = function(x) { g.e.push(...[1,2,3]) } g.o()
TEST_3: CONST
const g = { e: [] } g.o = function(x) { g.e.push(...[1,2,3]) } g.o()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
TEST_1: VAR
TEST_2: LET
TEST_3: CONST
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
TEST_1: VAR
31904226.0 Ops/sec
TEST_2: LET
30944438.0 Ops/sec
TEST_3: CONST
29830982.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark outlined in the provided JSON assesses the performance of JavaScript variables defined using three different keywords: `var`, `let`, and `const`. Each of these keywords has distinct properties in terms of scope and mutability, influencing how they behave in JavaScript. ### Tested Options 1. **VAR**: ```javascript var g = { e: [] } g.o = function(x) { g.e.push(...[1,2,3]) } g.o(); ``` - In this test case, `var` is used to declare the variable `g`, which is globally scoped or function-scoped, depending on where it's declared. - Pros: - Flexible usage, as it can be redeclared and updated. - Cons: - Can lead to unintended behavior due to hoisting and lack of block scope, potentially causing bugs in larger applications. 2. **LET**: ```javascript let g = { e: [] } g.o = function(x) { g.e.push(...[1,2,3]) } g.o(); ``` - Here, `let` is used, making `g` block-scoped. - Pros: - Prevents redeclaration within its scope, helping to avoid bugs. Its block scope also makes it clear that the variable shouldn't be accessible outside of its defined context. - Cons: - While it has enhanced packing features, it is slightly less performant than `var` in many cases because of more complex scope handling. 3. **CONST**: ```javascript const g = { e: [] } g.o = function(x) { g.e.push(...[1,2,3]) } g.o(); ``` - In this test, `const` is employed. `g` is block-scoped and cannot be reassigned. - Pros: - Guarantees immutability of the reference to the variable (though the contents can still be modified if it's an object), leading to safer code. - Cons: - Constraints on reassignment can be limiting; attempting to reassign `g` would result in an error. ### Performance Results The performance results of the JavaScript execution represented in the benchmark provide a metric called "Executions Per Second," indicating how many times the benchmark code could be executed in one second: - `TEST_1: VAR` achieved **16,936,656 executions per second**. - `TEST_2: LET` achieved **16,714,196 executions per second**. - `TEST_3: CONST` achieved **16,501,599 executions per second**. In this specific benchmark, `var` performed the best, followed closely by `let`, and `const` was marginally slower than both. ### Conclusion This benchmark allows developers to assess the performance of variable declarations in JavaScript. While `var` shows the most raw execution speed in this scenario, the choice of variable declaration should also depend on the application's design requirements. Using `let` or `const` may incur a performance trade-off for the benefits they provide in preventing errors and enhancing maintainability. ### Other Alternatives Alternative methods to manage variable scope and performance include: - **Function-scoped variables**: Use of functions and closures to manage variable lifecycles. - **Using ES6 Classes**: Instead of using constants or variables directly, creating classes encapsulates data and functions. - **Modules**: ES6 modules allow better encapsulation and can help avoid global variable pollution. In general, the adoption of `let` and `const` is recommended in modern JavaScript development, as they enforce better coding practices, despite possible minor performance differences in this benchmark.
Related benchmarks:
const vs let vs var (fixed)
const vs let vs var (fixed) 234
sometest1
var-let-const2
var-let-const3
new Array(length).fill().map vs Array.from({ length }, callback)
undefined to boolean
Nullish vs If
Nullish vs If 2 lvl
Comments
Confirm delete:
Do you really want to delete benchmark?