'The time complexity of the map function
I heard the calculation amount of map function is O(1). But I can't understand the reason.
Solution 1:[1]
If I understand your question correctly, O(1) is the complexity of accesing one item. Array.map() in JS passes the function the current value and iterates through all of them, and takes the return value of the function and inserts it into the new array.
Therefore, the function loops through every object in the array, having a complexity of O(n).
For example:
[1, 2, 3].map(function (item) { return item + 1; });
Said function takes one item at a time, accessing the array n times (3).
EDIT: Looks like I misunderstood your question, my bad.
Solution 2:[2]
The inbuilt map method shares the input iterable across the CPU cores. For an iterable of size n, the average running time would be ?(n/num_cpu_cores)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Ahmed Baruwa |