You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. I suggest changing the type checks from "type_of.call" to the javascript instanceof operator.
For example "type_of(str) == ARR" becomes "str instanceof Array" . But I believe it will not work for strings for unknown for me reason. Here is a small benchmark. In my test the instanceof operator performs 30x+ times faster and it should be even faster in the actual code because of the longer scope chain there. Although the actual difference is small it is not good coding practice IMO to use string conversations for type checks and it involves calling functions, creating new scopes, comparing strings, etc which is all useless in this case. Also if you have 1,000,000 users and sum their added performance you will save lot of world's computing time and resources and power and co2 etc ;)
var type_of = Object.prototype.toString;
var arr = [];
var t = (new Date()).valueOf();
for(var i = 0; i < 1000000; ++i) {
type_of.call(arr) === '[object Array]';
}
console.log((new Date()).valueOf() - t);
t = (new Date()).valueOf();
for(var i = 0; i < 1000000; ++i) {
arr instanceof Array;
}
console.log((new Date()).valueOf() - t);
The text was updated successfully, but these errors were encountered:
Thanks for your suggestions. I will definetly have a look as
soon as I can spare some time. Expect to hear from me in about
two weeks.
So long,
aefxx
On 08.07.2011 15:18, bobef wrote:
Hi. I suggest changing the type checks from "type_of.call" to the javascript instanceof operator.
For example "type_of(str) == ARR" becomes "str instanceof Array" . But I believe it will not work for strings for unknown for me reason. Here is a small benchmark. In my test the instanceof operator performs 30x+ times faster and it should be even faster in the actual code because of the longer scope chain there. Although the actual difference is small it is not good coding practice IMO to use string conversations for type checks and it involves calling functions, creating new scopes, comparing strings, etc which is all useless in this case. Also if you have 1,000,000 users and sum their added performance you will save lot of world's computing time and resources and power and co2 etc ;)
var type_of = Object.prototype.toString;
var arr = [];
var t = (new Date()).valueOf();
for(var i = 0; i< 1000000; ++i) {
type_of.call(arr) === '[object Array]';
}
console.log((new Date()).valueOf() - t);
t = (new Date()).valueOf();
for(var i = 0; i< 1000000; ++i) {
arr instanceof Array;
}
console.log((new Date()).valueOf() - t);
Hi. I suggest changing the type checks from "type_of.call" to the javascript instanceof operator.
For example "type_of(str) == ARR" becomes "str instanceof Array" . But I believe it will not work for strings for unknown for me reason. Here is a small benchmark. In my test the instanceof operator performs 30x+ times faster and it should be even faster in the actual code because of the longer scope chain there. Although the actual difference is small it is not good coding practice IMO to use string conversations for type checks and it involves calling functions, creating new scopes, comparing strings, etc which is all useless in this case. Also if you have 1,000,000 users and sum their added performance you will save lot of world's computing time and resources and power and co2 etc ;)
var type_of = Object.prototype.toString; var arr = []; var t = (new Date()).valueOf(); for(var i = 0; i < 1000000; ++i) { type_of.call(arr) === '[object Array]'; } console.log((new Date()).valueOf() - t);
The text was updated successfully, but these errors were encountered: