<script>
function randomString()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for( var i=0; i < 20; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
var randomKey = [];
var randomVals = [];
for (var i=0; i < 1001000; i++) {
randomKey[i] = randomString();
randomVals[i] = randomString();
}
</script>
var map10k = new Map();
var map100k = new Map();
var map1000k = new Map();
for (var i=0; i < 10000; i++) {
map10k.set(randomKey[i], randomVals[i]);
}
for (var i=0; i < 100000; i++) {
map100k.set(randomKey[i], randomVals[i]);
}
for (var i=0; i < 1000000; i++) {
map1000k.set(randomKey[i], randomVals[i]);
}
for (var i=0; i < 1000; i++) {
var val = map10k.size;
}
for (var i=0; i < 1000; i++) {
var val = map100k.size;
}
for (var i=0; i < 1000; i++) {
var val = map1000k.size;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Size 10k | |
Size 100k | |
Size 1000k |
Test name | Executions per second |
---|---|
Size 10k | 13151.7 Ops/sec |
Size 100k | 13259.5 Ops/sec |
Size 1000k | 13378.7 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark is designed to measure the performance of JavaScript Map data structures. It creates three instances of Maps with different sizes (10,000, 100,000, and 1,000,000 entries) and iterates over each one to retrieve the size using the size
property. The goal is to compare the execution times of these iterations.
Options Compared
The benchmark compares the following options:
Pros and Cons of Each Approach
Each approach has its advantages and disadvantages:
Library and Purpose
The Map
object in JavaScript is used here as a data structure to store key-value pairs. It provides efficient methods for adding, removing, and retrieving elements, making it suitable for this type of benchmark.
Special JS Feature or Syntax
This benchmark does not explicitly use any special JavaScript features or syntax beyond the standard Map
data structure.
Other Alternatives
For benchmarking similar scenarios, other alternatives could be:
Benchmark Preparation Code Explanation
The script preparation code generates three instances of Maps with different sizes (10,000, 100,000, and 1,000,000 entries) using nested loops. It populates each Map with random key-value pairs for more realistic behavior. The randomString
function is used to generate unique keys for the maps.
Individual Test Cases Explanation
Each test case measures the execution time of retrieving the size property from a single iteration over a given-sized Map. The tests are named according to their respective map sizes (10k, 100k, and 1,000k).