lkang2 / glmatrix

Automatically exported from code.google.com/p/glmatrix
0 stars 0 forks source link

use === instead of == #9

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
not only is === more accurate it is a lot faster.

Original issue reported on code.google.com by jeroom832@gmail.com on 6 Jun 2010 at 8:20

GoogleCodeExporter commented 8 years ago
Nice one! Didn't know that. Then also use !== of course :)

Original comment by danielhe...@gmail.com on 6 Jun 2010 at 10:52

GoogleCodeExporter commented 8 years ago
Hm, can't confirm it.

Microbenchmark:

<html>
<script type="text/javascript">
var a = 10;
var d=new Date();
for(var x = 0; x < 10000000; x++) {
    if (a===9){}
}
var e = new Date() -d;
var f=new Date();
for(var b = 0; b < 10000000; b++) {
    if (a==9){}
}
var g = new Date() -f;
var h=new Date();
for(var x = 0; x < 10000000; x++) {
    if (a!==9){}
}
var i = new Date() -h;
var j=new Date();
for(var b = 0; b < 10000000; b++) {
    if (a!=9){}
}
var k = new Date() -j;

alert(e);
alert(g);
alert(i);
alert(k);
</script>
</html>

Original comment by danielhe...@gmail.com on 6 Jun 2010 at 11:00

GoogleCodeExporter commented 8 years ago
<html>
<script type="text/javascript">
var a = [10];
var d=new Date();
for(var x = 0; x < 10000000; x++) {
    if (a===[10]){}
}
var e = new Date() -d;
var f=new Date();
for(var b = 0; b < 10000000; b++) {
    if (a==[10]){}
}
var g = new Date() -f;
var h=new Date();
for(var x = 0; x < 10000000; x++) {
    if (a!==[9]){
    }
}
var i = new Date() -h;
var j=new Date();
for(var b = 0; b < 10000000; b++) {
    if (a!=[9]){}
}
var k = new Date() -j;

alert(e);
alert(g);
alert(i);
alert(k);
</script>
</html>

This is a better representation of the library, I think

Original comment by jeroom832@gmail.com on 6 Jun 2010 at 11:08

GoogleCodeExporter commented 8 years ago
It looks like it is slower (especially !==) I think because because it also 
must 
check the type bits with ===:

<html>
<script type="text/javascript">
var a = 10;
var n = 1000000000;
var d=new Date();
do {
    if (a===9){}
}
while(n--);
var e = new Date() -d;
var n = 1000000000;
var f = new Date();
do {
    if (a==9){}
}
while(--n);
var g = new Date() -f;
var n = 1000000000;
var h=new Date();
do {
    if (a!==9){}
}
while(n--);
var i = new Date() -h;
var n = 1000000000;
var j=new Date();
do {
    if (a!=9){}
}
while(n--);
var k = new Date() -j;

alert(e);
alert(g);
alert(i);
alert(k);
</script>
</html>

Original comment by danielhe...@gmail.com on 6 Jun 2010 at 12:09

GoogleCodeExporter commented 8 years ago
@jeroom 832 Seems like it is only faster when comparing references. I don't 
think
your test is very representative because you are comparing an array with an 
constant
array. 

Maybe this test is more representative for comparing references? It is indeed 
faster,
so looks like mat === dest will also be faster.

<html>
<script type="text/javascript">
var a = new WebGLFloatArray(16);
var z = a;
var d=new Date();
for(var x = 0; x < 1000000000; x++) {
    if (a===z){}
}
var e = new Date() -d;
var f=new Date();
for(var b = 0; b < 1000000000; b++) {
    if (a==z){}
}
var g = new Date() -f;
var h=new Date();
for(var x = 0; x < 1000000000; x++) {
    if (a!==z){
    }
}
var i = new Date() -h;
var j=new Date();
for(var b = 0; b < 1000000000; b++) {
    if (a!=z){}
}
var k = new Date() -j;

alert(e);
alert(g);
alert(i);
alert(k);
</script>
</html>

Original comment by danielhe...@gmail.com on 7 Jun 2010 at 9:24

GoogleCodeExporter commented 8 years ago

Original comment by Tojiro@gmail.com on 12 Jun 2010 at 5:20

GoogleCodeExporter commented 8 years ago
@danielheres - Unfortunately even with your latest benchmark I'm testing === as 
slower. What browser are you using? Maybe Minefield is happier with ===?

In any case, the fact that the results seem reasonably inconsistent gives me 
reason to hold off on this. I may revisit it later to see if the situation has 
changed (as it frequently does in browsers) but for the time being this one 
isn't going in.

Original comment by Tojiro@gmail.com on 12 Jun 2010 at 5:37