>
> On Tue, Jun 2, 2009 at 4:10 PM, Douglas Crockford <douglas@...> wrote:
> > But then someone showed be a case where new Array(n) is actually useful.
>
> Will you share this useful case, or must it remain a mystery?

Suppose you need to construct a string containing n asterisks.
It seems the fastest way to do that is

new Array(n + 1).join('*')

sandyhead25

By faster do you mean faster for the interpreter to execute or faster for the person can save characters by not using a loop? If you mean anything but the

Message 2 of 11
, Jun 4, 2009

0 Attachment

By faster do you mean faster for the interpreter to execute or faster for the person can save characters by not using a loop?

If you mean anything but the first condition I would say you should still ban the array constructor, because those couple extra characters a person must type is pale in comparison to the potential harm introduced by using a method prone to fault. The whole point of JSLint is to solve that problem literally by mandating style not prone to fault.

pauanyu

... For reference, here is the way to do this with the array literal: var foo = []; foo.length = n + 1; foo = foo.join( * );

Looking at this from a coding standpoint, I would say that Method 1 is a most elegant bit of code.

--- In jslint_com@yahoogroups.com, "sandyhead25" <austin.cheney@...> wrote:
>
> By faster do you mean faster for the interpreter to execute or faster for the person can save characters by not using a loop?
>
> If you mean anything but the first condition I would say you should still ban the array constructor, because those couple extra characters a person must type is pale in comparison to the potential harm introduced by using a method prone to fault. The whole point of JSLint is to solve that problem literally by mandating style not prone to fault.
>

Method 4 compared very similar to Method 1 in speed, and actually slightly faster. (approximately 10-40% in my tests)
It was not the fastest in every browser, but was the most consistent in performance across browsers.

I would suggest Method 4 for best performance, and Method 1 for cleanest.

pauanyu

... Very interesting. I find it unintuitive how using arrays to work with strings can actually be faster than working directly with strings... yet here we are.

>
> //Method 4
> var foo = [];
> foo.length = n + 1;
> foo = foo.join('*');
>
> Method 4 compared very similar to Method 1 in speed, and actually slightly faster. (approximately 10-40% in my tests)
> It was not the fastest in every browser, but was the most consistent in performance across browsers.
>
> I would suggest Method 4 for best performance, and Method 1 for cleanest.
>

Very interesting. I find it unintuitive how using arrays to work with strings can actually be faster than working directly with strings... yet here we are.

40% faster, you say? That's impressive, given how both appear to do exactly the same thing. It probably has to do with the fact that "new Array" does different things depending on what you pass to it.

Your message has been successfully submitted and would be delivered to recipients shortly.