The article (link below) suggests that using the length property on a string creates an object reference which unnecessarily slows the function down.
http://www.webreference.com/programming/javascript/jkm3/2.html
In this context, what is the advantage of using lodash _.size() ? Does it perform any differently than the (native...?) length property?
If you're counting an array or keys in an object, is there any benefit to using lodash size instead of the length property?
From the lodash sources, _.size()
is implemented as:
function size(collection) {
var length = collection ? getLength(collection) : 0;
return isLength(length) ? length : keys(collection).length;
}
For an array, the first line is indirectly doing collection.length
so that _.size()
is, if anything, a little (tiny) bit slower.
In the performance article, the performance problem is that the property lookup of length
is being used when a number on the stack could have been used to achieve the same goal. In other words, the solution was not to look for a faster property, but to avoid the property altogether when it could be done.