Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
pushIfNotExist 2
(version: 1)
Comparing performance of:
pushIfNotExist vs pushIfNotExistSet
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function pushIfNotExist(array, element, key="id"){ if (!element) return array; const isArray = Array.isArray(element); const isElementSet = false; const isElementMap = false; if (isArray || isElementSet || isElementMap) { throw Error( "pushIfNotExist : Type of element is invalid. Types array, map or set are not supported." ); } const isPrimitive = ["string", "boolean", "number"].includes(typeof element); const isElementInArray = isPrimitive ? array.includes(element) : array.some((elementFromArray) => element[key] === elementFromArray[key]); if (isElementInArray) return array; array.push(element); return array; } function pushIfNotExistSet(array, element, key="id"){ if (!element) return array; const isArray = Array.isArray(element); const isElementSet = false; const isElementMap = false; if (isArray || isElementSet || isElementMap) { throw Error( "pushIfNotExist : Type of element is invalid. Types array, map or set are not supported." ); } const isPrimitive = ["string", "boolean", "number"].includes(typeof element); let isElementInArray = false; const arraySet = new Set(array.map((elementFromArray) => elementFromArray[key])); isElementInArray = arraySet.has(element); if (isElementInArray) return array; array.push(element); return array; }
Tests:
pushIfNotExist
const array = [...Array(1000).keys()] const result = pushIfNotExist(array, 1001)
pushIfNotExistSet
const array = [...Array(1000).keys()] const result = pushIfNotExistSet(array, 1001)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
pushIfNotExist
pushIfNotExistSet
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):
I'll break down the provided benchmark definition and test cases, explaining what's being tested, compared, and discussed. **Benchmark Definition** The provided JSON represents two JavaScript microbenchmarks: 1. `pushIfNotExist`: Tests the performance of the `pushIfNotExist` function, which adds an element to an array if it doesn't already exist. 2. `pushIfNotExistSet`: Tests the performance of the `pushIfNotExistSet` function, which adds an element to a set (an unordered collection of unique elements) if it doesn't already exist. **Options Compared** The two benchmarks compare different approaches for checking if an element is already present in the array or set: * In `pushIfNotExist`, the function uses the `includes()` method to check if the element is already present in the array. If the element is a primitive (string, boolean, number), it checks for presence using `array.includes(element)`. Otherwise, it checks for presence by iterating over each element in the array and comparing properties. * In `pushIfNotExistSet`, the function uses a `Set` data structure to check if the element is already present. It creates a set of unique elements from the original array and then checks if the element is present in this set using `arraySet.has(element)`. **Pros and Cons** Here's a brief analysis of each approach: * **`includes()` method:** * Pros: * Fast and efficient for primitive types (strings, numbers) * Easy to implement * Cons: * Slower for non-primitive types (objects) due to property comparison * May not be suitable for large datasets or complex data structures * **Set-based approach:** * Pros: * Fast and efficient for checking presence in a set of unique elements * Suitable for large datasets and complex data structures * Cons: * Requires more memory to store the set * More complex implementation **Other Considerations** * **Data structure choice:** Depending on the specific use case, either an array or a set might be a better choice. Arrays are suitable for ordered collections of elements, while sets are ideal for unordered collections of unique elements. * **Cache friendliness:** If the `pushIfNotExist` function is called repeatedly with the same element, using a cache (e.g., a `Set`) can improve performance by avoiding repeated checks. **Library Usage** The provided code uses several libraries or data structures: * `Array.isArray()`: Checks if an object is an array. * `Set`: Used to create a set of unique elements from the original array. * `includes()` and `some()`: Methods used for checking presence in arrays. **Special JS Features or Syntax** None mentioned explicitly, but the use of modern JavaScript features like `const` declarations, arrow functions (`=>`), and template literals (`\r\n`) is evident. These features are part of ECMAScript 2015 (ES6) and later standards. **Alternative Approaches** Other possible approaches for checking presence in an array or set include: * Using a more advanced data structure like a trie or a prefix tree. * Implementing a custom hash-based search algorithm. * Utilizing GPU acceleration through libraries like WebGL or OpenCL.
Related benchmarks:
conact vs push
comparing push vs spread one by one
Array.prototype.slice vs spread operator - objects 2
Array.prototype.slice vs spread operator - objects 22
Clone and append to array
Comments
Confirm delete:
Do you really want to delete benchmark?