What is the big O of Array.protoype.filter
?
I have looked at the documentation (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) but haven't been able to work it out.
At this moment, the time complexity is O(n), linear time, where n is the size of fahrenheit array. Then we apply the result of map function to filter function. The time complexity of the filter function is O(n) as well.
The time complexity of Array. prototype. find is O(n) (with n being the array's length), and it's fair to assume that it will remain that way. Generally speaking, it's often impossible for engines to improve the complexity class of an operation.
To our surprise, for-loops are much faster than the Array. filter method. To be precise, the Filter method is 77% slower than for loop.
O(N)
Example:
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => isBig(word));
function isBig(word){
console.log("called");
return word.length > 6;
}
Output:
"called" "called" "called" "called" "called" "called" Array ["exuberant", "destruction", "present"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With