Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array from() vs Map.keys()
(version: 0)
Comparing performance of:
Array.from vs Map.keys
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Array.from
var fooMap = new Map(); for(var i=0;i<10000;i++) { fooMap.set(i, i); } var other = [...fooMap];
Map.keys
var fooMap = new Map(); for(var i=0;i<10000;i++) { fooMap.set(i, i); } var other = fooMap.keys();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.from
Map.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0
Browser/OS:
Firefox 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.from
919.5 Ops/sec
Map.keys
1599.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Definition** The benchmark is defined by two test cases: `Array.from()` and `Map.keys()`. These are both methods used to convert an iterable (like an array or map) into a new array or object. The main difference between them lies in how they approach this conversion: * `Array.from()`: This method creates a new array from an iterable or an array-like object. It takes an iterable as an argument and returns a new array with the elements of that iterable. * `Map.keys()`: This method returns a iterator over the keys of a map (an object that stores key-value pairs). This iterator can be used to create a new iterable, like an array. **Options Compared** The benchmark is comparing two approaches: 1. Using `Array.from()` to convert a map into an array. 2. Using `Map.keys()` directly to get an iterator over the keys of the map and then creating an array from that iterator. **Pros and Cons** Here are some pros and cons for each approach: * **Using `Array.from()`**: * Pros: * More concise code * May be faster in some cases (because it avoids the overhead of using `Map.keys()`) * Cons: * Creates a new array, which may consume more memory for large inputs. * Does not take advantage of any special properties of the input map that could make the operation more efficient. * **Using `Map.keys()`**: * Pros: * Does not create a new array, so it can be more memory-efficient than using `Array.from()`. * Can potentially be faster because it avoids creating an intermediate array. * Cons: * Requires the input to be a map (which may not always be the case). * May be slower because of the overhead of working with iterators. **Library Used** In this benchmark, none are explicitly stated as used by test users. However, in JavaScript `Map` and its methods like `keys()` and `values()`, are built-in. **Special JS Feature or Syntax** The feature used here is that of using `Map` object, which is a fundamental part of the JavaScript standard library for working with collections of key-value pairs. It was introduced in ECMAScript 2015 (ES6). **Other Alternatives** There are several other ways to convert an iterable into a new array or object: * **Using `Array.prototype.slice()` or `Array.from().slice()`**: These methods create a shallow copy of the input array, but they can be slower than using `Array.from()`. * **Using `String.prototype.split()`**: This method splits a string into an array of substrings. * **Using `Array.prototype.map()` and `Array.prototype.reduce()`**: These methods can be used to transform an iterable into an array. However, they are more computationally expensive than using `Array.from()`.
Related benchmarks:
Array.from() vs new Array() vs [..Array()]
Array.from() vs new Array() - map
Array.from() vs new Array().map()
Array.from() vs new Array() with index
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?