Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
const vs let vs var
(version: 0)
Comparing performance of:
var vs let vs const
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
var
var g = { e: [] } g.o = function(x) { g.e.push(...[1,2,3]) } g.o()
let
var g = { e: [] } g.o = function(x) { g.e.push(...[1,2,3]) } g.o()
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
var
let
const
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 140 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
var
6321395.5 Ops/sec
let
6483139.5 Ops/sec
const
6368289.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain what's tested in the provided JSON benchmark. The test measures the performance difference between three JavaScript variable declarations: `const`, `let`, and `var`. The script is designed to push an array of numbers onto a nested object using two different methods: assigning directly to the property (`g.e`) versus calling a function that modifies the same property (`g.o()`). **Options compared:** 1. `const` (constant): Declares a variable that cannot be reassigned. 2. `let`: Declares a variable that can be reassigned, but not within its own block scope (i.e., inside an `if` or `loop` statement). 3. `var`: Declares a variable that can be reassigned anywhere in the function. **Pros and cons of each approach:** * `const`: + Pros: Memory efficiency, because the variable cannot be changed, which reduces the likelihood of unintended side effects. + Cons: Cannot be reassigned; if the value is needed to be modified, a new constant should be created. * `let`: + Pros: More efficient than `var`, since it redeclares the variable's scope and creates a new context. + Cons: Not suitable for functions or loops where reassignment might occur. * `var`: + Pros: Can be used in any context, but it can lead to unintended side effects due to global scope. + Cons: Memory inefficiency, as variables are stored on the global scope, and their values can affect other parts of the code. **Library usage:** None There is no external library used in this benchmark. **Special JS features/syntax:** None mentioned There's no special JavaScript feature or syntax being tested in this benchmark. Now, let's discuss some alternative approaches to testing performance differences between these variable declarations: 1. **Simple loop**: Create a simple loop that assigns and reassigns values using each variable declaration. 2. **Object iteration**: Create an object with nested properties and iterate over it using each variable declaration. 3. **Array manipulation**: Create an array of numbers and use `map()`, `filter()`, or other methods to modify its contents, comparing the performance differences between `const`, `let`, and `var`. 4. **Nested function calls**: Create a nested function that modifies variables declared with each keyword. These alternatives can provide more comprehensive insights into the performance characteristics of each variable declaration.
Related benchmarks:
var vs. const vs. let
var vs let vs const init
let vs const in tight loops
var vs const vs let
let vs const vs var
Comments
Confirm delete:
Do you really want to delete benchmark?