for(i=0; i<1000; i++){
let an = i;}
for(i=0; i<100000; i++){
var an = i;}
for(i=0; i<100000; i++){
let an = i;}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Varrr | |
Lettt |
Test name | Executions per second |
---|---|
Varrr | 3871.8 Ops/sec |
Lettt | 3867.3 Ops/sec |
Measuring JavaScript performance is an essential task for any web developer or performance engineer. MeasuringThat.net provides a platform to create and run microbenchmarks, allowing users to compare the performance of different approaches.
What's being tested?
In this benchmark, two variables are being compared: var
and let
. The test case uses a simple loop that increments a variable from 0 to 100000. The focus is on how each keyword affects the execution time.
Options compared:
There are two options being compared:
for
loops).let
is a new keyword for declaring variables that have scoped closure.Pros and Cons:
Variable Hoisting:
In JavaScript, variables declared using var
are "hoisted" to the top of their scope. This means that even though you write for (let i = 0; i < 100000; i++)
, the variable i
is actually moved to the top of the script and initialized with a value of undefined
. In contrast, let
variables are not hoisted in the same way.
Library usage:
There doesn't seem to be any explicit library used in this benchmark. However, it's worth noting that some browsers may use their own libraries or engines for JavaScript execution.
Special JS feature or syntax:
This benchmark doesn't explicitly test any special JavaScript features or syntax, such as ES6 modules, async/await, or promises.
Other alternatives:
If you're interested in exploring alternative approaches to measuring performance, consider the following:
node --inspect
flag or WebAssembly-based interpreters.Keep in mind that these alternatives might require additional setup and expertise, especially if you're targeting older browsers or environments.
In summary, this benchmark provides a straightforward comparison between var
and let
variables in JavaScript. By testing their performance, developers can gain insights into the impact of variable declarations on execution time.