A classic case is a list of primitives, e.g. strings, or numbers, and a check we would like to perform without creating a new function each time.
// recycled function
function alreadyThere(value) {
// wanted coercion, no cast needed
return value == this;
// shitty code, logically speaking
// one cast per iteration
return value === "" + this;
}
var listOfNames = ["Me", "You", "Others"];
// the pointless check
listOfNames.some(
// the recycled callback
alreadyThere,
// the *passed as object* this
"You"
); // will be true
Now, for above specific case anyone would use an indexOf but this is not the point.
The point is that in some case we may want to do more complicated stuff and compare the result with this
// know if word was already in the dictionary
function alreadyThere(value) {
return value.toLowerCase() == this;
}
// convert the check once per iteration
listOfNames.some(
alreadyThere,
"yoU".toLowerCase()
); // still true
It's About Optimizations
The whole point is to try to perform as less runtime computations as possible.Following this logic, we may decide to lower-case all items in the list once, and never again, but unfortunately this will require duplicated amount of RAM used per each collection.
The String#toLowerCase() is fast enough for a not so frequent check so why bother the RAM?
The optimization is also to avoid this.toLowerCase() per each entry of the dictionary.
The concept here is again simple, avoid duplicated entries in a generic list of chars, but things may be similar with numbers.
Performances
I have created a specific test able to understand performances gap between coercion and cast per iteration.Surprising Google Chrome seems to be able to optimize in core once per iteration the cast, resulting almost twice as fast as competitors.
Specially on mobile, the in core coercion seems to be faster, sometimes almost twice as fast as the cast per iteration is.
Note that the toString method seems to give better perfs (see the unpublished test http://jsperf.com/some-coercion/2). Nevertheless, I totally agree with you.
ReplyDelete