var set = new Set();
var obj = new Object();
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
set.add(makeid());
set.has(makeid())
obj[makeid()]=i;
if (obj.hasOwnProperty(makeid())) ;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set | |
Object |
Test name | Executions per second |
---|---|
Set | 488370.4 Ops/sec |
Object | 482588.3 Ops/sec |
Let's break down what's being tested in this benchmark.
The provided JSON represents two test cases for comparing the performance of JavaScript sets and objects.
Options Compared:
set.add(makeid())
and then checking if that same element exists in the set using set.has(makeid())
.obj[makeid()] = i
and then checking if that same element has a property on the object using if (obj.hasOwnProperty(makeid()))
.Pros and Cons:
hasOwnProperty()
can be slower because it requires iterating through all properties of the object.Library Purpose:
The library used in this benchmark is not explicitly mentioned, but it's likely that the JavaScript engine being tested provides support for these data structures. The Set
and Object
constructors are part of the standard JavaScript API, which means they are implemented by the JavaScript engine itself.
Special JS Feature/Syntax:
makeid()
to generate random numbers. This is likely used to create unique identifiers for elements being added to the Set or object.Other Alternatives:
If you're interested in exploring alternative data structures for testing, here are some options:
Set
class in Node.js.In conclusion, this benchmark tests the performance of adding elements to Sets and objects, highlighting their differences in execution time. By using a custom function for generating unique identifiers, it simulates real-world scenarios where sets are used to maintain uniqueness.