Live data from Hacker News

Advanced Javascript tips and tricks

code.google.com

31–37 of 37 posts

Re: Advanced Javascript tips and tricks

#31

list = list.sort(function() Math.random() - 0.5); Please don't shuffle an array like this. Array.prototype.sort requires the comparison function to be referentially transparent (i.e. to always return the same result when given the same two elements to compare), otherwise the sort order is undefined. For example, imagine how far from "shuffled" the resulting array will be if a bubble sort is used with a randomised com…

The European Browser Choice screen was a good example of where this style of sorting can come and bite you - the browser choice screen was designed to display a selection of browsers in random order, but the above sorting code unintentionally biased it towards particular browsers.

Here's a good writeup with a bunch of test runs and graphs: http://www.robweir.com/blog/2010/02/microsoft-random-browser...

Re: Advanced Javascript tips and tricks

#32

I find some of them horribly obscure +new Date() // 1259359833574 Oh come on, magic constants? How do you check if that's the right number? function PrefixInteger(num, length) { return (num / Math.pow(10, length)).toFixed(length).substr(2); } Is there no sprintf in Javascript? Or did I misunderstand what the function does?

"1259359833574" isn't a magic constant. JavaScript uses // for comments. The number is just an example of the possible output of the expression "+new Date()" (specifically, it was the time at which the author of that page evaluated the expression).

Re: Advanced Javascript tips and tricks

#33
post #32

I find some of them horribly obscure +new Date() // 1259359833574 Oh come on, magic constants? How do you check if that's the right number? function PrefixInteger(num, length) { return (num / Math.pow(10, length)).toFixed(length).substr(2); } Is there no sprintf in Javascript? Or did I misunderstand what the function does?

"1259359833574" isn't a magic constant. JavaScript uses // for comments. The number is just an example of the possible output of the expression "+new Date()" (specifically, it was the time at which the author of that page evaluated the expression).

Right, and it's equivalent to the more readable

    Number(new Date())
or more simply

    Date.now()
if you don't need IE8 support.

Re: Advanced Javascript tips and tricks

#34

Earlier quoted context omitted.

Fisher-Yates is a better idea, of course, since it is pretty short and runs in linear time, but if someone insists on a sort-based one-linear this should work: list.map(function (t) { return [Math.random(), t]; }).sort().map(function (t) { return t[1]; })

Argh, I missed the edit time window: "one-linear", should be, of course, "one-liner". Fisher-Yates is O(n), but the sort makes this algorithm Omega(n log n).

What's 'Omega'?

Re: Advanced Javascript tips and tricks

#35

Earlier quoted context omitted.

Argh, I missed the edit time window: "one-linear", should be, of course, "one-liner". Fisher-Yates is O(n), but the sort makes this algorithm Omega(n log n).

What's 'Omega'?

omega() is like the opposite of O(). it means "at least" rather than "at most". — http://en.wikipedia.org/wiki/Big_Oh_notation#Family_of_Bachm...

Re: Advanced Javascript tips and tricks

#36
post #7

list = list.sort(function() Math.random() - 0.5); Please don't shuffle an array like this. Array.prototype.sort requires the comparison function to be referentially transparent (i.e. to always return the same result when given the same two elements to compare), otherwise the sort order is undefined. For example, imagine how far from "shuffled" the resulting array will be if a bubble sort is used with a randomised com…

While I agree in principle, there are many cases where you don't care about the statistical quality of a random shuffle, where brevity has its benefit.

When? It's not like anyone is asking you to write the shuffling code by hand, so when is time so precious, and space so constrained, that it's beneficial to use this "brief" incorrect solution over taking the 5 minutes to google a correct solution and copy-pasting that?
Post reply on HN