Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Objects vs Array 2
(version: 0)
Comparing performance of:
Includes vs Object vs Set
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var set = new Set(); var obj = {}; var array = []; var size = 1000000; var test = (function (length, probability) { function random() { return Math.random().toString(36).substr(2); } var i; for (i = 0; i < size; i++) { var str = random(); set.add(str); obj[str] = true; array[i] = str; } var test = []; test.length = length; var c = size / probability; for (i = 0; i < length; i++) { var index = Math.floor(Math.random() * c); test[i] = index < size ? array[index] : random(); } return test; })(10000, 0.5);
Tests:
Includes
var count = 0; for (var i = 0; i < test.length; i++) { if (array.includes(test[i])) { count++; } }
Object
var count = 0; for (var i = 0; i < test.length; i++) { if (!!test[i]) { count++; } }
Set
var count = 0; for (var i = 0; i < test.length; i++) { if (set.has(test[i])) { count++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Includes
Object
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on MeasureThat.net. **Benchmark Definition** The provided JSON represents a benchmark definition for three test cases: "Set", "Object", and "Includes". The script preparation code sets up an environment with: 1. A set (`var set = new Set();`), an object (`var obj = {};`), and an array (`var array = [];`) 2. A large dataset of 1,000,000 strings (randomly generated) 3. Three variables: `length`, `probability`, and `test` The script defines a function that generates the test data based on the `length` and `probability` parameters. **Test Cases** Each test case has a unique benchmark definition in the "Benchmark Definition" field. Here's what each test case does: 1. **Includes**: Iterates through the test array and checks if each element is present in the original array using the `includes()` method. 2. **Object**: Iterates through the test array and checks if each element has a truthy value (i.e., not null, undefined, or 0) by using the unary `!!` operator. 3. **Set**: Iterates through the test array and checks if each element is present in the set using the `has()` method. **Options Compared** The three test cases compare different approaches to check for presence: 1. **Includes**: Uses the `includes()` method, which iterates through the original array to find a match. 2. **Object**: Uses the unary `!!` operator to check if an element is truthy. This approach relies on JavaScript's loose typing and the fact that empty objects are considered falsey. 3. **Set**: Uses the `has()` method, which provides a more efficient way to check for presence in a set. **Pros and Cons** Here's a brief summary of each approach: 1. **Includes**: * Pros: Simple and straightforward implementation. * Cons: Iterates through the original array, making it slower for large datasets. 2. **Object**: * Pros: Relies on JavaScript's loose typing and provides a simple implementation. * Cons: May not work as expected for certain edge cases (e.g., NaN values) and relies on the unary `!!` operator's behavior. 3. **Set**: * Pros: Provides an efficient way to check for presence in a set, especially when the dataset is large. * Cons: Requires creating a set instance, which may incur additional overhead. **Library and Special JS Features** There are no notable libraries or special JavaScript features being used in this benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, consider the following options: 1. **Using `in` operator**: Instead of using `includes()`, you could use the `in` operator to check if an element is present in the array. 2. **Using a custom implementation**: You could implement your own iterative approach to check for presence, which might provide better performance for specific use cases. 3. **Using other data structures**: Depending on your requirements, you might consider using other data structures like a hash table or a trie to improve lookup performance. I hope this explanation helps you understand what's being tested in the MeasureThat.net benchmark!
Related benchmarks:
Set.prototype.has() vs "in" operator
Set vs Objects vs Array
Sets, Objects, Arrays Performance
JavaScript spread operator vs Object.assign performance, large objects v2
Comments
Confirm delete:
Do you really want to delete benchmark?