<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
var routes = [];
var numRoutes = 10000;
var lines = 500;
for(var i = 1; i <= numRoutes; i++){
var numLines = Math.round(Math.random() * lines);
var lineIds = [];
for(var j = 0; j <= numLines; j++) {
lineIds.push(Math.round(Math.random() * lines))
}
routes.push({route_id: i, ids: lineIds});
}
var lines = {};
routes.forEach(function(d) {
d.ids.forEach(function(e) {
lines[e] = (lines[e] || 0) + 1;
});
});
console.log(lines)
var lines = {};
for(var i = 1; i <= lines; i++) {
lines[i] = 0;
}
routes.forEach(function(d) {
d.ids.forEach(function(e) {
lines[e] = lines[e] + 1;
});
});
console.log(lines)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Stock | |
Alternative |
Test name | Executions per second |
---|---|
Stock | 16.2 Ops/sec |
Alternative | 14.5 Ops/sec |
I'll break down the provided benchmark definition and individual test cases, explaining what is tested, the options compared, pros and cons of each approach, and other considerations.
Benchmark Definition
The benchmark definition represents a JavaScript microbenchmark that tests object creation performance. The script preparation code creates an array routes
with 10,000 elements, where each element contains an array ids
with a random number of elements between 1 and 500. The goal is to create an object lines
that maps the ids
from the routes
array to their frequency count.
The HTML preparation code includes a reference to jQuery version 3.3.1, which might be used for DOM manipulation or other purposes in the benchmark.
Individual Test Cases
There are two test cases:
var lines = {};
routes.forEach(function(d) {
d.ids.forEach(function(e) {
lines[e] = (lines[e] || 0) + 1;
});
});
console.log(lines);
This code uses the forEach
method to iterate over the ids
array and update the lines
object. The (lines[e] || 0)
expression is a shorthand for checking if the key e
exists in the lines
object, returning 0 if it doesn't.
var lines = {};
for(var i = 1; i <= lines; i++) {
lines[i] = 0;
}
routes.forEach(function(d) {
d.ids.forEach(function(e) {
lines[e] = lines[e] + 1;
});
});
console.log(lines);
This code uses a traditional for
loop to initialize the lines
object with zeros, and then uses the same forEach
method as in the "Stock" test case.
Options Compared
The two test cases differ in their approach to creating the lines
object:
: Uses
forEach`, which is a modern JavaScript method that iterates over an array or iterable object.: Uses a traditional
for` loop, which can be slower and more verbose.Pros and Cons
forEach
)for
loopsOther Considerations
lines
object is created with an initial size of 0, which means that the first time a key is accessed, the value will be set to 1. This can affect performance depending on the specific use case.Alternatives
Other alternatives for creating the lines
object could include:
Map
instead of an object: var lines = new Map();
Keep in mind that these alternatives might not be relevant to the specific benchmark definition and test cases provided.