<!--your preparation HTML code goes here-->
/*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();
}
var g = { e: [] }
g.o = function(x) { g.e.push( [1,2,3]) }
g.o()
let g = { e: [] }
g.o = function(x) { g.e.push( [1,2,3]) }
g.o()
const g = { e: [] }
g.o = function(x) { g.e.push( [1,2,3]) }
g.o()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
TEST_1: VAR | |
TEST_2: LET | |
TEST_3: CONST |
Test name | Executions per second |
---|---|
TEST_1: VAR | 16936656.0 Ops/sec |
TEST_2: LET | 16714196.0 Ops/sec |
TEST_3: CONST | 16501599.0 Ops/sec |
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.
VAR:
var g = { e: [] }
g.o = function(x) { g.e.push(...[1,2,3]) }
g.o();
var
is used to declare the variable g
, which is globally scoped or function-scoped, depending on where it's declared. LET:
let g = { e: [] }
g.o = function(x) { g.e.push(...[1,2,3]) }
g.o();
let
is used, making g
block-scoped.var
in many cases because of more complex scope handling.CONST:
const g = { e: [] }
g.o = function(x) { g.e.push(...[1,2,3]) }
g.o();
const
is employed. g
is block-scoped and cannot be reassigned.g
would result in an error.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.
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.
Alternative methods to manage variable scope and performance include:
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.