tag:blogger.com,1999:blog-34454975.post3861213857678964784..comments2020-12-31T07:44:01.206+01:00Comments on Web Reflection: Two simple tricks in JavaScript ( olds, but always useful )Andrea Giammarchihttp://www.blogger.com/profile/16277820774810688474noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-34454975.post-34193731148200359302008-11-10T06:28:00.000+01:002008-11-10T06:28:00.000+01:00The bitwise operators are only equivalent to Math....The bitwise operators are only equivalent to Math.floor for n >= 0<BR/><BR/>Math.floor(-10.7); // -11<BR/>-10.7 >> 0.5; // -10<BR/>-10.7 | 0; // -10<BR/><BR/>Also, imo they tend to obscure the code for future maintainers. I can't think of any operations in js that would be performing the amount of Math calls needed to yield a noticeable degradation in user experience.<BR/><BR/>Incidentally, in some benchmarks I did a while back, I found that storing the global Math (or a specific Math function) in a local or closure var alleviates most/all of the performance penalty.<BR/><BR/>That said, I certainly appreciate the tips! It's always good to see clever code patterns, regardless if you'll use them. I'd played with the ~ for a while, but didn't find a use for it. I'll probably stick to stored Math functions and simple comparators for the benefit of those who inherit my code... but maybe the ~index thing will slip into a personal project or two :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-34454975.post-38466404927514329572008-06-22T19:46:00.000+02:002008-06-22T19:46:00.000+02:00I think in daily real applications, it is always p...I think in daily real applications, it is always possible.<BR/>Anyway, I will talk about binaries and numeric JS limits, in another post :)Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-9774899179113843222008-06-22T19:36:00.000+02:002008-06-22T19:36:00.000+02:00Andrea Giammarchi, you can use arrays :)Not all re...Andrea Giammarchi, you can use arrays :)<BR/>Not all readers of your blog know about this limit for using binary operations.<BR/>They have to be know that use "i>>1" in place of "Math.floor(i/2)" possible not always.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-34454975.post-45216971177313586092008-06-22T12:03:00.000+02:002008-06-22T12:03:00.000+02:00Yes Mega, I don't know why I did not think to writ...Yes Mega, I don't know why I did not think to write an example with <I>num >> .5</I> ;)Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-91234016549761690592008-06-22T10:22:00.000+02:002008-06-22T10:22:00.000+02:00Great!I also think we can totally replace Math.flo...Great!<BR/><BR/>I also think we can totally replace Math.floor with:<BR/><BR/>number>>0.5<BR/><BR/>for example:<BR/><BR/>19.46543 >> 0.5<BR/><BR/>returns 19Lorenzohttps://www.blogger.com/profile/17052539879440146404noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-76561052317618081672008-06-07T21:58:00.000+02:002008-06-07T21:58:00.000+02:00gregof, your example does not make sense ... that ...gregof, your example does not make sense ... that is the tipical Math.pow(2, 31) limit and does not mean that we have to be careful with >> operator.<BR/><BR/>First of all, because I suppose there are rare cases where we use such big integers with JavaScript, secondly, because your comment is, basically, like this one:<BR/><BR/>be careful with Arrays length:<BR/><BR/><I>var a = new Array(Math.pow(2, 32));<BR/>a.push(123);// Error, invalid array length<BR/>a.lengt++; // Error, invalid array length</I><BR/><BR/>Does it mean that we canot use arrays? :roll:Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-86857336321719441472008-06-06T20:52:00.000+02:002008-06-06T20:52:00.000+02:00Be careful using big numbers!2147483648 >> 1 //-10...Be careful using big numbers!<BR/>2147483648 >> 1 //-1073741824Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-34454975.post-43601039238233153012008-06-06T11:49:00.000+02:002008-06-06T11:49:00.000+02:00Yep great stuff!More please :)Yep great stuff!<BR/>More please :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-34454975.post-37918522131505613022008-06-06T05:59:00.000+02:002008-06-06T05:59:00.000+02:00Interesting.I always considered bitwise operators ...Interesting.<BR/><BR/>I always considered bitwise operators slow, but your testcase proves it wrong.Anonymousnoreply@blogger.com