function calc(x, y, z) {
return Math.sqrt(x**2 + y**2 + z**2);
}
for (var i = 0; i < 100; i++) {
var a = 1;
var b = 2;
var c = 2.1;
calc(a, b, c);
}
for (var i = 0; i < 100; i++) {
var a = 1,
b = 2,
c = 2.1;
calc(a, b, c);
}
for (var i = 0; i < 100; i++) {
var t = {
a: 1,
b: 2,
c: 2.1
}
calc(t.a, t.b, t.c);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Normal | |
Normal 2 | |
Associative array |
Test name | Executions per second |
---|---|
Normal | 41839.4 Ops/sec |
Normal 2 | 45047.2 Ops/sec |
Associative array | 46040.7 Ops/sec |
Let's break down the provided JSON data and explain what is being tested.
Benchmark Definition
The benchmark definition is a simple JavaScript function calc
that calculates the square root of the sum of squares of three variables using the Math library. The function takes three arguments: x
, y
, and z
.
Script Preparation Code
The script preparation code provides a concrete implementation of the calc
function:
function calc(x, y, z) {
return Math.sqrt(x**2 + y**2 + z**2);
}
This code defines the calc
function with the specified signature.
Html Preparation Code
There is no HTML preparation code provided in this benchmark definition. This means that the benchmark does not rely on any HTML-specific features or libraries.
Individual Test Cases
The benchmark consists of three test cases, each representing a different variable declaration approach:
for (var i = 0; i < 100; i++) { var a = 1; var b = 2; var c = 2.1; calc(a, b, c); }
This test case declares the variables a
, b
, and c
using the traditional var
keyword.for (var i = 0; i < 100; i++) { var a = 1, b = 2, c = 2.1; calc(a, b, c); }
This test case uses the shorthand syntax to declare multiple variables on the same line.for (var i = 0; i < 100; i++) { var t = { a: 1, b: 2, c: 2.1 }; calc(t.a, t.b, t.c); }
This test case uses an associative array (object literal) to declare the variables a
, b
, and c
.Library
The Math library is used in the calc
function to calculate the square root.
Special JS Features/Syntax
None of the provided code uses any special JavaScript features or syntax, such as ES6 classes, modules, or async/await.
Pros and Cons
Here are some pros and cons of each variable declaration approach:
var
syntax.Other Alternatives
If the benchmark were to test other variable declaration approaches, some alternatives could be:
However, these alternatives would likely require additional modifications to the calc
function and might not provide a fair comparison with the original variable declaration approaches.
Benchmark Considerations
When creating benchmarks like this one, it's essential to consider the following factors:
By taking these considerations into account, the MeasureThat.net team has created a comprehensive benchmarking framework for JavaScript microbenchmarks like this one.