Re: JSLint and JavaScript: The Good Parts.



When reading into this issue somebody suggested === would be faster than ==, I just finished running a very basic benchmark comparing == vs === as far as speed is concerned (string comparison). Unsurprisingly there isn't any speed difference, however the uncertainty of the results in === was far bigger (0.05 s with == and 0.3 s with ===), could anybody check whether this difference is reproducible or whether this is only some random problem with my system. (Tested with chromium on mac). I ran the following script 10 times for both == and ===, it's might be a bit simplistic, but there would have any big difference it should have come out.

<script>
start = (new Date()).getTime();
string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
string2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
for(i=0;i<10000;i++){
if(string===string2){
     string=string+" ";
     string2=string2+" ";
     
}
}

        stop = (new Date()).getTime();
console.log(stop-start);
</script>

David Mulder

On Mon, Mar 15, 2010 at 4:20 PM, David Mulder <greatslovakia zoho com> wrote:
When reading into this issue somebody suggested === would be faster than ==, I just finished running a very basic benchmark comparing == vs === as far as speed is concerned (string comparison). Unsurprisingly there isn't any speed difference, however the uncertainty of the results in === was far bigger (0.05 s with == and 0.3 s with ===), could anybody check whether this difference is reproducible or whether this is only some random problem with my system. (Tested with chromium on mac). I ran the following script 10 times for both == and ===, it's might be a bit simplistic, but there would have any big difference it should have come out.

<script>
start = (new Date()).getTime();
string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
string2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
for(i=0;i<10000;i++){
if(string===string2){
     string=string+" ";
     string2=string2+" ";
     
}
}

        stop = (new Date()).getTime();
console.log(stop-start);
</script>

David Mulder

On Mon, Mar 15, 2010 at 4:03 PM, Owen Taylor <otaylor redhat com> wrote:
On Sat, 2010-03-13 at 13:21 +1100, Lex Hider wrote:

> However, this one (== vs ===) does seem to be a very worthwhile change
> to the style guide to consider.
> Wouldn't it be clearer if code depending on (0 != null but undefined
> ==null) be made explicit?

I've (in the past) read what Crockford has to say on the issue, and
didn't find it convincing.

Yes, the coercion behavior of the standard '==' is surprising
at times (strings to numbers in particular), but that only happens
of you are comparing items that are in fact of different types. Which is
generally not good programming practice.

But if you use '===', then you have to have an accurate mental model of
where _javascript_ uses 'undefined' and where it uses 'null', and that's
hard and obscure.

And also, you make the code look strange and intimidating.

So, my rule is that you should only use === and !== if it actually makes
a difference and you should try to avoid situations where it actually
makes a difference.

- Owen



_______________________________________________
gnome-shell-list mailing list
gnome-shell-list gnome org
http://mail.gnome.org/mailman/listinfo/gnome-shell-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]