function aLoop(nodes, fn) {
for (var i=0, len=nodes.length; i<len; i++) {
fn(nodes[i], i, nodes);
}
}
var nodes = document.querySelectorAll('div');
var arr1 = [1,2,3];
var classs;
aLoop(nodes, function(elm){
aLoop(arr1, function(item){
classs = elm.className;
elm.className = classs+'temp';
});
});
aLoop(nodes, function(elm){
classs = elm.className;
aLoop(arr1, function(item){
classs = classs+'temp';
});
elm.className = classs.trim();
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Get-Set object prop in 2-each-times | |
Get-Set-trim in 1-each-time |
Test name | Executions per second |
---|---|
Get-Set object prop in 2-each-times | 1323.9 Ops/sec |
Get-Set-trim in 1-each-time | 1330.2 Ops/sec |
Benchmark Overview
The provided JSON represents a benchmark test for JavaScript performance, specifically focused on getting and setting an HTML element's class property. The test consists of two individual test cases: "Get-Set object prop in 2-each-times" and "Get-Set-trim in 1-each-time".
Test Case 1: Get-Set object prop in 2-each-times
This test case involves calling the aLoop
function twice, once for each array element (arr1
). In the inner loop, a class property is set to a new value by concatenating it with "temp". Then, the original class property is retrieved from the outer loop's context and assigned to the element.
Test Case 2: Get-Set-trim in 1-each-time
This test case involves calling the aLoop
function once, where after setting a new value for the class property (similarly as in Test Case 1), the trimmed version of the original class property is set on the element.
Options Compared
The two test cases compare the following approaches:
Pros and Cons
trim
method, which may be more efficient for string manipulation.Library Usage
The test case utilizes a custom function called aLoop
, but no external libraries are required. However, it does rely on JavaScript's built-in document.querySelectorAll
method to select HTML elements.
Special JS Feature or Syntax
None mentioned in this explanation.