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 = []; 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.indexOf(temp[i]) === -1) // remove duplicates unique.push(temp[i]); } for (var i = 0; i < unique.length; i++) { // split the strings back into array uniqueCoords = uniqueCoords.concat(unique[i].split('|')); }
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):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmarking test case, where two different approaches are compared to create a unique set of coordinates and then split them into an array. **Script Preparation Code** The script preparation code generates an array `coordinates` with 1000 elements, each representing a pair of random angles in degrees (ranging from -180 to 180). The angles are converted to long integers and multiplied by 10^7 before being assigned to the `coordinates` array. ```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) } ``` This code creates a large array of random coordinate pairs. **Html Preparation Code** There is no HTML preparation code provided in the benchmark definition. **Test Cases** There are two test cases: ### 1. "Array from - Set" ```javascript var unique = Array.from(new Set(coordinates.reduce((r, a, i) => (i % 2 ?\r\n r[r.length - 1].push(a) : r.push([a]), r), []).map(a => a.join('|'))), s => s.split('|').map(Number)); ``` This test case uses the `Array.from()` method to create an array from a set of unique coordinate pairs. The set is created by reducing the `coordinates` array, pushing each element into an array, and then joining them with pipes (`|`) using `join()`. The resulting string is then split into an array of numbers using `split()` and `map()`. **Pros:** * Easy to read and understand * Uses built-in methods for creating sets and arrays **Cons:** * May not be as efficient as other approaches, especially for large datasets * Creates a new array with duplicate elements if the input set contains duplicates ### 2. "For loops" ```javascript 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.indexOf(temp[i]) === -1) // remove duplicates unique.push(temp[i]); } for (var i = 0; i < unique.length; i++) { // split the strings back into array uniqueCoords = uniqueCoords.concat(unique[i].split('|')); } ``` This test case uses two `for` loops to create a set of unique coordinate pairs. The first loop creates an array of strings by concatenating each pair of coordinates with pipes (`|`). The second loop removes duplicates from the array using `indexOf()` and `push()`. Finally, the third loop splits each string back into an array of numbers. **Pros:** * Does not create any additional arrays or sets * Does not use built-in methods for creating sets **Cons:** * More complex and harder to read than the first approach * May be less efficient due to manual iteration over the data **Library Used:** None. The benchmark uses only standard JavaScript functions and variables. **Special JS Features/Syntax:** The benchmark uses some advanced JavaScript features, such as: * `Array.from()` method (ES6) * `Set` object (ES6) * `join()` method * `split()` method with a callback function However, these features are not specific to the problem being solved and can be used in other contexts. **Alternatives:** Other approaches could use different methods for creating sets, such as: * Using the `Set` constructor directly * Using a library like Lodash's `uniqueValue()` function * Implementing a custom set data structure However, the benchmark definition provided does not specify any alternatives or variations on the test cases.
Related benchmarks:
unique coordinates
unique coordinates
unique coordinates
unique coordinates
Comments
Confirm delete:
Do you really want to delete benchmark?