Except that, if you are using JS for web development, as most of us, 99% of the times you will be working with arrays containing tens of objects, and theese optimizations will be irrelevant.
I've often heard "there will never be more than 20 items on this page" followed by "let's add this complex metadata to each item" followed by "shit, due to unforeseen circumstances, a big client needs 20000 items on that page, and now it feels sluggish".
I'd say don't spend time on difficult optimizations with negligible real world improvements... But when the optimization is extremely easy, why would you NOT do it?
exportfunctionmap(fn,thisArg){constO=Object(this)constlen=O.lengthif(!(fninstanceofFunction))thrownewTypeError(`${fn} is not a function`)constA=newArray(len)for(letk=0;k<len;k++){constPk=k.toString()constkPresent=O.hasOwnProperty(Pk)if(kPresent){constkValue=O[i]constmappedValue=fn.call(thisArg,kValue,i,O)Object.defineProperty(A,Pk,{value:mappedValue,writable:true,enumerable:true,configurable:true})}}returnA}
But most of that is lost unless you actually write the upper loop inline, because unlike the built in methods it gets deoptimized if you call the utility with many different fn functions. Especially when some of them throw.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Except that, if you are using JS for web development, as most of us, 99% of the times you will be working with arrays containing tens of objects, and theese optimizations will be irrelevant.
I've often heard "there will never be more than 20 items on this page" followed by "let's add this complex metadata to each item" followed by "shit, due to unforeseen circumstances, a big client needs 20000 items on that page, and now it feels sluggish".
I'd say don't spend time on difficult optimizations with negligible real world improvements... But when the optimization is extremely easy, why would you NOT do it?
I wish JS runtimes did better inlining and monomorphization.
Then you could have your own utilities like
Which beats out what the spec method does:
But most of that is lost unless you actually write the upper loop inline, because unlike the built in methods it gets deoptimized if you call the utility with many different
fn
functions. Especially when some of them throw.