On Sun, Mar 20, 2011 at 7:29 AM, אריה גלזר <[email protected]> wrote: > IMO this is a very good example of over optimizing. Choose the one that's > most readable and shorter to write (= push() ). > Array performance is simply not something you should care about in 99% of JS > apps. Unless you actually find yourself pushing 10K items into an array (in > which case it's almost certain that you can use caching), thinking of > whether or not to use push is simply a waste of your time. > Don't do premature optimization or you'll be wasting your time on the wring > stuff. > > AFAIK arrays are extremely well optimized on all modern browsers, and as for > IE, well, it'll be slow no matter what you do, but optimizing array item > addition will not make it faster >
Just to make sure I am not misunderstood. Array are also used to do image manipulations with canvas. 10K items is really a very tiny picture (very small array), I am talking about 200K / 500K items in my case :) That's one reason why "apply()" cannot handle that on every browser, and yes n[n.length] is faster than ".push()" since the cost of the call is avoided and replaced by a simpler lookup. Don't worry @arieh, I am not loosing my time and I am actually doing just fine thanks. You should better ignore messages that do not interest you ! Or just ask details if you don't understand ;-) -- Diego > On Thu, Mar 17, 2011 at 12:36 AM, Diego Perini <[email protected]> > wrote: >> >> On Wed, Mar 16, 2011 at 8:39 PM, Tony Wang <[email protected]> wrote: >> > Oops , I find this script is too heavy for my ie8 but it's fine for >> > firefox. >> > (that's just like what we know .... ) >> > >> > Don't click it if you are using IE. haha >> > >> > This is a smaller amount version. >> > http://jsfiddle.net/We9p9/3/ >> > >> > And the speed still very depends on your browser implemention between >> > n[n.length] and n.push(). >> > >> > >> > 2011/3/16 Tony Wang <[email protected]> >> >> >> >> btw ,there's a interesting topic ,too. >> >> >> >> if you are doing array concat job, you could take a look for this. >> >> http://jsfiddle.net/We9p9/1/ >> >> >> >> The keypoint is , even when you are using array.push , >> >> >> >> ary.push(1,2,3,4,5); >> >> is still faster then >> >> ary.push(1); >> >> ary.push(2); >> >> ary.push(3); >> >> ary.push(4); >> >> ary.push(5); >> >> >> >> >> >> >> >> 2011/3/16 Diego Perini <[email protected]>: >> >> > On Wed, Mar 16, 2011 at 12:14 PM, Rob Griffiths <[email protected]> >> >> > wrote: >> >> >> >> >> >>> Length should return the value of the last index + 1, so in that >> >> >>> case >> >> >>> length will return 8 >> >> >> >> >> >> I knew I should have checked before stating that. >> >> >> >> >> >> -- >> >> >> Rob Griffiths >> >> >> http://bytespider.eu >> >> >> @bytespider >> >> >> https://github.com/bytespider >> >> >> >> >> >> -- >> >> >> To view archived discussions from the original JSMentors Mailman >> >> >> list: >> >> >> http://www.mail-archive.com/[email protected]/ >> >> >> >> >> >> To search via a non-Google archive, visit here: >> >> >> http://www.mail-archive.com/[email protected]/ >> >> >> >> >> >> To unsubscribe from this group, send email to >> >> >> [email protected] >> >> >> >> >> > >> >> > I use the following to convert/concat NodeList to Array: >> >> > >> >> > // concat elements to data array >> >> > concatList = >> >> > function(data, elements) { >> >> > var i = -1, element; >> >> > if (data.length === 0 && Array.slice) >> >> > return Array.slice(elements); >> >> > while ((element = elements[++i])) >> >> > data[data.length] = element; >> >> > return data; >> >> > }, >> >> > >> >> > On older browsers this was the fastest "loop" among many browsers at >> >> > the time I tested it and on newer browsers this is not >> >> > used/necessary. >> >> > >> >> > I am not sure using Firefox "Array.slice" shortcut when "data" array >> >> > is empty is worth it (nowadays) so the bare bone JS should be this: >> >> > >> >> > // concat elements to data array >> >> > concatList = >> >> > function(data, elements) { >> >> > var i = -1, element; >> >> > while ((element = elements[++i])) >> >> > data[data.length] = element; >> >> > return data; >> >> > }, >> >> > >> >> > At that time another interesting thing I observed was that Webkit >> >> > based browser were also faster to access DOM element in the ancient >> >> > way using the "nodelist.item(n)" notation (really much faster). >> >> > >> >> > The point is performances are improving at a fast pace in every >> >> > browser engine so small speed improvements like these are becoming >> >> > less and less relevant, it is still a funny exercise though :) >> >> > >> >> > -- >> >> > Diego >> >> > >> >> > -- >> >> > To view archived discussions from the original JSMentors Mailman >> >> > list: >> >> > http://www.mail-archive.com/[email protected]/ >> >> > >> >> > To search via a non-Google archive, visit here: >> >> > http://www.mail-archive.com/[email protected]/ >> >> > >> >> > To unsubscribe from this group, send email to >> >> > [email protected] >> >> > >> >> >> > >> > -- >> > To view archived discussions from the original JSMentors Mailman list: >> > http://www.mail-archive.com/[email protected]/ >> > >> > To search via a non-Google archive, visit here: >> > http://www.mail-archive.com/[email protected]/ >> > >> > To unsubscribe from this group, send email to >> > [email protected] >> > >> >> Tony, >> "apply()" seems a bit faster with big numbers, however the number of >> arguments you can pass are limited in all browsers. That's one reason >> I try to avoid it when I know I will maybe hit those limits. >> >> You will easily hit those limits with string to array conversions in >> canvas for example, were I found the problem first time I tried. In >> other cases I think the current limitations are not a problem :) >> >> I tweeted about that before and there are exact numbers in that tweet >> for Safari/Chrome which are differently limited (even if both are >> Webkit implementations). >> >> Firefox and Opera seems to have a higher limit if I recall correctly >> almost, twice as much (something around 256K for WK and 512K/1M for >> FF/OP). >> >> -- >> Diego >> >> -- >> To view archived discussions from the original JSMentors Mailman list: >> http://www.mail-archive.com/[email protected]/ >> >> To search via a non-Google archive, visit here: >> http://www.mail-archive.com/[email protected]/ >> >> To unsubscribe from this group, send email to >> [email protected] > > > > -- > Arieh Glazer > אריה גלזר > 052-5348-561 > http://www.arieh.co.il > http://www.link-wd.co.il > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
