Re: [jslint] Re: new Array(3)
- Very interesting test :-)
thanx jsperf ;-)
but there was some errors...
To be comparable, in those scripts
str = a.join('char');
str = a.join('*');
str = a.join(char);
Please note also that priority must not be always focused on perf
These must also be considered:
- Readability of the code
- Less code === Less potential error hidden in the pattern (wrong copy/paste or error while typing)
So I may be more confident to see these powerful patterns only where they are required
(maybe even with a comment to improve the readability)
About these two readable solutions, the use of "new Array(n)" vs "a.length = n"
I still prefer the first one...
var tmpArray, myString;
tmpArray = ;
tmpArray.length = 20;
myString = tmpArray.join('*');
-> The initialisation of my string variable require to use a temporary variable which will pollute my scope when debugging
myString = ;
myString.length = 20;
myString = myString('*');
-> myString is first an array (arghhh) before becoming the desired string... and still multiple lines for simple initialisation
var myString = new Array(10).join('*');
-> The initialisation is on one line only and directly provide the good type
On Nov 30, 2010, at 11:56 AM, Marcel Duran wrote:
> > http://tech.groups.yahoo.com/group/jslint_com/message/614
> > Provides an elegant & effective solution without the use of new Array(n+1)
> > and it operates quickly in all modern browsers.
> > I've just made a JSPerf benchmark comparing these various ways of creating
> a string of n repeated character:
> Seems like new Array and .length are the slowest ones at least in latest
> version of Chrome. Feel free to run this test in different browsers and let
> browserscope take care of benchmark results.
> [Non-text portions of this message have been removed]
[Non-text portions of this message have been removed]
- --- In firstname.lastname@example.org, Morgaut Alexandre Louis Marc <morgaut@...> wrote:
> Please note also that priority must not be always focused on perfReadability is paramount. So the inherent off-by-one problem in the trick makes it hazardous. Certainly it does not justify keeping new Array in the safe subset. The fact that new Array(3) is not the same as  is a more serious problem than the need to make a string with the wrong number of stars in it.
> These must also be considered:
> - Readability of the code
> - Less code === Less potential error hidden in the pattern (wrong copy/paste or error while typing)
Is there another essential use for new Array?