Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Jewels And Stones Leetcode
(version: 0)
https://leetcode.com/problems/jewels-and-stones/description/
Comparing performance of:
for of loop vs hashmap vs for loop
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
for of loop
function findJewels (jewels, stones) { // iterate through our stones.. if it is a jewel, increment let count = 0; for (const char of stones) { if (jewels.includes(char)) count++; } return count; }; for (i = 0; i < 10; i++) { console.log(findJewels("aA", "aAAbbbb"), "3"); console.log(findJewels("z", "ZZ"), "0"); }
hashmap
function findJewels(jewels, stones) { const map = {}; let count = 0; for (const jewel of jewels) { map[jewel] = true; } for (const stone of stones) { if (map[stone]) { count++; } } return count; } for (i = 0; i < 10; i++) { console.log(findJewels("aA", "aAAbbbb"), "3"); console.log(findJewels("z", "ZZ"), "0"); }
for loop
function findJewels(jewels, stones) { // declare a count & length variable let count = 0; let len = stones.length; // Iterate through the longest array stones. Increment count by 1 if it exists in jewels for (let i = 0; i < len; i++) { if (jewels.indexOf(stones[i]) >= 0) { count++; } } return count; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for of loop
hashmap
for loop
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):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark, titled "Jewels And Stones Leetcode," tests the performance of different approaches to solving a problem on LeetCode. **Test Cases** There are three test cases: 1. **For Loop**: This approach uses a traditional `for` loop to iterate through the stones array and check if each stone is present in the jewels array. 2. **Hashmap**: This approach uses a JavaScript object (HashMap) to store the jewels as keys and increment a counter for each stone that matches a jewel. 3. **For of Loop**: This approach uses the `for...of` loop to iterate through the stones array and check if each stone is present in the jewels array. **Options Compared** The three approaches are compared in terms of their performance, which is measured by the number of executions per second (ExecutionsPerSecond). **Pros and Cons of Each Approach** 1. **For Loop**: * Pros: Simple to understand and implement. * Cons: May be slower than other approaches due to the overhead of checking for element presence in the `indexOf` method. 2. **Hashmap**: * Pros: Can be faster than traditional `for` loops, especially for large datasets, since it uses a data structure optimized for lookups. * Cons: Requires additional memory for storing the HashMap and may have slower initialization times. 3. **For of Loop**: * Pros: Similar to the traditional `for` loop approach but with improved performance due to faster iteration through the array using the `for...of` loop syntax. * Cons: May be less intuitive for developers without experience with this syntax. **Other Considerations** * The use of `console.log` statements in the benchmark definition code may introduce additional overhead and should be avoided in production code. * The test cases do not account for edge cases, such as an empty jewels array or a stones array with duplicate elements. **Library Used** In the "Hashmap" test case, JavaScript object (HashMap) is used. This is a built-in data structure in JavaScript that allows for efficient storage and lookup of key-value pairs. **Special JS Feature/Syntax** The "For of Loop" test case uses the `for...of` loop syntax, which is a newer feature introduced in ECMAScript 2015 (ES6). This syntax provides faster iteration through arrays using iterators, which can improve performance compared to traditional `for` loops.
Related benchmarks:
Unique via Set vs Filter
indexof vs hash
IndexOf vs Includes vs hash vs set
Hash vs insert
Group by
Comments
Confirm delete:
Do you really want to delete benchmark?