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< 1e4; 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 = []; var uniqueCoords = []; for (var i = 0; i < coordinates.length; i += 2) { temp.push(coordinates[i] + '|' + coordinates[i + 1]); // create some strings } for (var i = 0; i < temp.length; i++) { if ( !(unique.includes(temp[i]))) { // remove duplicates unique.push(temp[i]); uniqueCoords.push(temp[i].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):
I'll break down the provided benchmarking test cases and explain what's being tested. **Benchmark Definition JSON** The `Script Preparation Code` section defines a JavaScript function that generates an array of unique coordinates. The coordinates are created using `Math.random()` to generate random values between -180 and 180 degrees, multiplied by 1e7 (100 million), rounded down, and then converted back to degrees. **Options Compared** Two different approaches are compared: 1. **Using Array.from() with Set**: This approach uses the `Array.from()` method to create an array from a set of unique coordinates. The set is created by reducing the array of coordinates into a set using the `Set` constructor, which automatically removes duplicates. Then, `Array.from()` converts the set back into an array. 2. **Using For Loops**: This approach uses two nested for loops to create an array of unique coordinates. It first creates an array of strings by concatenating each pair of coordinates with a '|' separator. Then, it iterates through this array and pushes each string to the `unique` array only if it's not already present. **Pros and Cons** 1. **Using Array.from() with Set**: * Pros: More concise and efficient, as it avoids unnecessary iterations. * Cons: May be slower due to the overhead of creating a set and converting it back into an array. 2. **Using For Loops**: * Pros: Can be more intuitive for developers who are familiar with for loops, as it avoids using higher-order functions like `Array.from()` and Set. * Cons: More verbose and potentially slower due to the need for two nested iterations. **Library and Purpose** There is no specific library mentioned in the benchmark definition. However, the use of `Array.from()` suggests that it's a modern JavaScript feature introduced in ECMAScript 2015 (ES6). **Special JS Feature or Syntax** None are explicitly used in this benchmarking test case.
Related benchmarks:
unique coordinates
unique coordinates
unique coordinates
unique coordinates
Comments
Confirm delete:
Do you really want to delete benchmark?