Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique coordinates
(version: 0)
Comparing performance of:
Array from - Set vs For loops
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var coordinates = []; for (var i=0; i< 1000; i++) { coordinates.push(Math.round((Math.random()*360-180)*1e7)/1e7, Math.round((Math.random()*360-180)*1e7)/1e7) }
Tests:
Array from - Set
var unique = Array.from(new Set(coordinates.reduce((r, a, i) => (i % 2 ? r[r.length - 1].push(a) : r.push([a]), r), []).map(a => a.join('|'))), s => s.split('|').map(Number));
For loops
var temp = []; var unique = []; for (var i = 0; i < coordinates.length; i += 2) { var val = coordinates[i] + '|' + coordinates[i + 1]; if (temp.indexOf(val) === -1) { temp.push(val); unique.push( val.split('|').map(Number) ) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array from - Set
For loops
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 break down the provided JSON and benchmark. **Benchmark Definition** The benchmark is designed to measure performance differences between two approaches: using `Array.from` with a `Set` or manually iterating through the data. The script generates an array of unique coordinates, which are then processed by each approach. **Script Preparation Code** The script generates an array of 1000 unique coordinates using `Math.random()`: ```javascript for (var i = 0; i < 1000; i++) { coordinates.push(Math.round((Math.random() * 360 - 180) * 1e7) / 1e7, Math.round((Math.random() * 360 - 180) * 1e7) / 1e7); } ``` **Html Preparation Code** There is no HTML preparation code provided. **Individual Test Cases** ### Array.from - Set ```javascript var unique = Array.from(new Set(coordinates.reduce((r, a, i) => (i % 2 ? r[r.length - 1].push(a) : r.push([a]), r), []).map(a => a.join('|'))), s => s.split('|').map(Number)); ``` This test case uses `Array.from` to convert an array-like object into an actual array, and then uses a `Set` to remove duplicates. The coordinates are reduced (merged) using the modulo operator (`i % 2`) and then mapped to create unique strings by joining the coordinates with the pipe character (`|`). Finally, each string is split and mapped to numbers. **Pros:** * Efficient use of memory since it only stores unique coordinates. * Fast iteration through the data using `Array.from` and `Set`. **Cons:** * Requires modern JavaScript features (e.g., `Array.from`, `Set`) which may not be supported in older browsers or environments. ### For Loops ```javascript for (var i = 0; i < coordinates.length; i += 2) { var val = coordinates[i] + '|' + coordinates[i + 1]; if (temp.indexOf(val) === -1) { temp.push(val); unique.push(val.split('|').map(Number)); } } ``` This test case uses traditional `for` loops to iterate through the data. It creates a temporary array (`temp`) and pushes unique values into it, and then splits each value by pipe character (`|`) and maps to numbers. **Pros:** * Can be executed in older browsers or environments that don't support modern JavaScript features. * Does not require additional libraries or modules. **Cons:** * Less efficient memory usage since it stores all coordinates, even if they are duplicates. * Slower iteration through the data due to manual indexing and looping. **Library and Special JS Features** There is no library used in this benchmark. However, some modern browsers may support the `Set` data structure or `Array.from()` method without any issues. **Other Alternatives** 1. **Regular Expressions**: Instead of using a `Set`, you could use regular expressions to remove duplicates. This would be slower and less efficient than the `Set` approach. 2. **Manual Hashing**: You could implement your own hashing algorithm (e.g., MD5, SHA-256) to remove duplicates. This would require more complex code and likely slower performance. 3. **Other Iteration Methods**: Other iteration methods like `forEach()` or `for...of` loop could be used instead of traditional `for` loops. Keep in mind that the performance differences between these approaches may vary depending on the specific use case, hardware, and browser environment.
Related benchmarks:
unique coordinates
unique coordinates
unique coordinates
unique coordinates
Comments
Confirm delete:
Do you really want to delete benchmark?