Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small performance suggestions #3

Open
ghost opened this issue Jul 8, 2011 · 1 comment
Open

Small performance suggestions #3

ghost opened this issue Jul 8, 2011 · 1 comment

Comments

@ghost
Copy link

ghost commented Jul 8, 2011

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);
@aefxx
Copy link
Owner

aefxx commented Jul 10, 2011

Hi Bob.

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant