<script type="text/javascript" src="https://publicstatic.tableausoftware.com/vizql/v_100001608191615/javascripts/mscorlib.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
var newCss = {};
newCss["test1"] = "test1val";
newCss["test2"] = "test2val";
newCss["test3"] = "test3val";
newCss["test4"] = "test4val";
newCss["test5"] = "test5val";
newCss["test6"] = "test6val";
newCss["test7"] = "test7val";
newCss["test8"] = "test8val";
var temp = {};
var keyss = Object.keys(newCss);
for (var $t2 = 0; $t2 < keyss.length; $t2++) {
var key1 = keyss[$t2];
temp[key1] = newCss[key1];
}
var temp = {};
var $t1 = ss.getEnumerator(Object.keys(newCss));
try {
while ($t1.moveNext()) {
var key = $t1.current();
temp[key] = newCss[key];
}
}
finally {
$t1.dispose();
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Underscore (array like) | |
Saltarelle foreach |
Test name | Executions per second |
---|---|
Underscore (array like) | 548627.4 Ops/sec |
Saltarelle foreach | 401059.9 Ops/sec |
Let's dive into the explanation of the provided benchmark.
Benchmark Overview
The benchmark compares two approaches for iterating over keys in an object: Underscore.js (specifically, its "array like" approach) and Saltarelle foreach.
Underscore.js Approach
In the first test case, the benchmark uses Underscore.js to iterate over the keys of newCss
:
var keyss = Object.keys(newCss);
for (var $t2 = 0; $t2 < keyss.length; $t2++) {
var key1 = keyss[$t2];
temp[key1] = newCss[key1];
}
This code uses the Object.keys()
method to get an array of keys, and then loops through it using a traditional for loop. The resulting object temp
is populated with values from newCss
.
Saltarelle foreach Approach
The second test case uses Saltarelle foreach to iterate over the keys:
var $t1 = ss.getEnumerator(Object.keys(newCss));
try {
while ($t1.moveNext()) {
var key = $t1.current();
temp[key] = newCss[key];
}
} finally {
$t1.dispose();
}
This code uses the getEnumerator()
method to create an enumerator for the keys, and then loops through it using a while loop. The resulting object temp
is populated with values from newCss
.
Options Compared
The benchmark compares two options:
Object.keys()
method to get an array of keys and then loops through it.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Other Considerations
Additional Notes
The benchmark also includes a reference to an external library (mscorlib.js
), which seems to be a tableau-specific library for JavaScript execution. The Underscore.js
library used in the benchmark is version 1.8.3, which is an older version of the library.