I have a situation where I need to update a pair of divs every second (a countdown timer). But depending on the user''s actions, those divs may or may not exist in the DOM. I was prepared to do this: if ( $$(''div.myStyle'').size() > 0 ) { // do stuff } But it occurred to me that $$ could be a computationally expensive operation to be running every second. Thoughts? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
jarrod wrote:> But it occurred to me that $$ could be a computationally expensive > operation to be running every second.Ummm... did you benchmark it? -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
No - and I hadn''t thought of that. I can benchmark it. But I don''t really have anything to compare it against. I just wondered if - theoretically - the $$ operation was expensive. Let me see what benchmarking says... On Sep 18, 8:52 am, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> jarrod wrote: > > But it occurred to me that $$ could be a computationally expensive > > operation to be running every second. > > Ummm... did you benchmark it? > > -- > Michael Peters > Developer > Plus Three, LP--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Well here you go, I learn something new every day. console.profile(''double$''); var pe = new PeriodicalExecuter(function(){ var c = []; c = $$(''div.myStyle''); },5) console.profileEnd(''double$''); I set the profiler to run every 5 seconds while I played with the interface, changed the DOM, etc... repeatedly, I got times in the <20ms range. Most times registered at 0ms. So either the profiler is broken, or the DOM is cached and can be parsed extremely quickly. To really stress the $$() function, I also tested this: console.profile(''double$''); var c = []; for (var i=0; i<100; i++) { c = $$(''div.myStyle''); } console.profileEnd(''double$''); That registered a time of only 202ms, so that''s not bad. Thanks for the help! On Sep 18, 8:57 am, jarrod <jarrod.carl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> No - and I hadn''t thought of that. I can benchmark it. But I don''t > really have anything to compare it against. > > I just wondered if - theoretically - the $$ operation was expensive. > > Let me see what benchmarking says... > > On Sep 18, 8:52 am, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote: > > > jarrod wrote: > > > But it occurred to me that $$ could be a computationally expensive > > > operation to be running every second. > > > Ummm... did you benchmark it? > > > -- > > Michael Peters > > Developer > > Plus Three, LP--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Make sure you try it in more than just Firefox, which has excellent speed due to built-in XPath functions. You may also want to try variations on your CSS selector; for example, add the id of a parent node so only a subset of the document is searched. TAG On Sep 18, 2007, at 7:29 AM, jarrod wrote:> > Well here you go, I learn something new every day. > > console.profile(''double$''); > var pe = new PeriodicalExecuter(function(){ > var c = []; > c = $$(''div.myStyle''); > },5) > console.profileEnd(''double$''); > > > I set the profiler to run every 5 seconds while I played with the > interface, changed the DOM, etc... repeatedly, I got times in the > <20ms range. Most times registered at 0ms. So either the profiler is > broken, or the DOM is cached and can be parsed extremely quickly. > > To really stress the $$() function, I also tested this: > > console.profile(''double$''); > var c = []; > for (var i=0; i<100; i++) { > c = $$(''div.myStyle''); > } > console.profileEnd(''double$''); > > That registered a time of only 202ms, so that''s not bad. > > > Thanks for the help! > > > > > On Sep 18, 8:57 am, jarrod <jarrod.carl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> No - and I hadn''t thought of that. I can benchmark it. But I don''t >> really have anything to compare it against. >> >> I just wondered if - theoretically - the $$ operation was expensive. >> >> Let me see what benchmarking says... >> >> On Sep 18, 8:52 am, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote: >> >>> jarrod wrote: >>>> But it occurred to me that $$ could be a computationally expensive >>>> operation to be running every second. >> >>> Ummm... did you benchmark it? >> >>> -- >>> Michael Peters >>> Developer >>> Plus Three, LP > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I don''t think your timer would create a lot of lag by collecting one or two objects on a twenty second interval. Where the DOM extension functions really slow you down is in iteration. I recently was building a date chooser gadget and I was using the "down" method for retrieving a cell by row index and column index. In an iteration over the table this slowed things down extremely. Granted this is a rather extreme case of iteration and DOM functions, but it does have a cost that will add up on you.>But I don''t really have anything to compare it against.You don''t have to compare it against anything, if it slows down your interface look to improve things. If you can see it lag it is taking too long, anything over 200 ms and your users will notice. On Sep 18, 10:41 am, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote:> Make sure you try it in more than just Firefox, which has excellent > speed due to built-in XPath functions. > > You may also want to try variations on your CSS selector; for > example, add the id of a parent node so only a subset of the document > is searched. > > TAG > > On Sep 18, 2007, at 7:29 AM, jarrod wrote: > > > > > Well here you go, I learn something new every day. > > > console.profile(''double$''); > > var pe = new PeriodicalExecuter(function(){ > > var c = []; > > c = $$(''div.myStyle''); > > },5) > > console.profileEnd(''double$''); > > > I set the profiler to run every 5 seconds while I played with the > > interface, changed the DOM, etc... repeatedly, I got times in the > > <20ms range. Most times registered at 0ms. So either the profiler is > > broken, or the DOM is cached and can be parsed extremely quickly. > > > To really stress the $$() function, I also tested this: > > > console.profile(''double$''); > > var c = []; > > for (var i=0; i<100; i++) { > > c = $$(''div.myStyle''); > > } > > console.profileEnd(''double$''); > > > That registered a time of only 202ms, so that''s not bad. > > > Thanks for the help! > > > On Sep 18, 8:57 am, jarrod <jarrod.carl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> No - and I hadn''t thought of that. I can benchmark it. But I don''t > >> really have anything to compare it against. > > >> I just wondered if - theoretically - the $$ operation was expensive. > > >> Let me see what benchmarking says... > > >> On Sep 18, 8:52 am, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote: > > >>> jarrod wrote: > >>>> But it occurred to me that $$ could be a computationally expensive > >>>> operation to be running every second. > > >>> Ummm... did you benchmark it? > > >>> -- > >>> Michael Peters > >>> Developer > >>> Plus Three, LP--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
1. Why don''t you use ID instead of className? 2. Why aren''t you smarter ? - You have an index - Each time a DIV is created then update the index - Each time a Div is removed then clear the index => Don''t worry, Be lazy ;-) On 9/18/07, Matt Foster <mattfoster01-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I don''t think your timer would create a lot of lag by collecting one > or two objects on a twenty second interval. > > Where the DOM extension functions really slow you down is in > iteration. I recently was building a date chooser gadget and I was > using the "down" method for retrieving a cell by row index and column > index. In an iteration over the table this slowed things down > extremely. Granted this is a rather extreme case of iteration and DOM > functions, but it does have a cost that will add up on you. > > >But I don''t really have anything to compare it against. > > You don''t have to compare it against anything, if it slows down your > interface look to improve things. If you can see it lag it is taking > too long, anything over 200 ms and your users will notice. > > > > On Sep 18, 10:41 am, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote: > > Make sure you try it in more than just Firefox, which has excellent > > speed due to built-in XPath functions. > > > > You may also want to try variations on your CSS selector; for > > example, add the id of a parent node so only a subset of the document > > is searched. > > > > TAG > > > > On Sep 18, 2007, at 7:29 AM, jarrod wrote: > > > > > > > > > Well here you go, I learn something new every day. > > > > > console.profile(''double$''); > > > var pe = new PeriodicalExecuter(function(){ > > > var c = []; > > > c = $$(''div.myStyle''); > > > },5) > > > console.profileEnd(''double$''); > > > > > I set the profiler to run every 5 seconds while I played with the > > > interface, changed the DOM, etc... repeatedly, I got times in the > > > <20ms range. Most times registered at 0ms. So either the profiler is > > > broken, or the DOM is cached and can be parsed extremely quickly. > > > > > To really stress the $$() function, I also tested this: > > > > > console.profile(''double$''); > > > var c = []; > > > for (var i=0; i<100; i++) { > > > c = $$(''div.myStyle''); > > > } > > > console.profileEnd(''double$''); > > > > > That registered a time of only 202ms, so that''s not bad. > > > > > Thanks for the help! > > > > > On Sep 18, 8:57 am, jarrod <jarrod.carl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> No - and I hadn''t thought of that. I can benchmark it. But I don''t > > >> really have anything to compare it against. > > > > >> I just wondered if - theoretically - the $$ operation was expensive. > > > > >> Let me see what benchmarking says... > > > > >> On Sep 18, 8:52 am, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote: > > > > >>> jarrod wrote: > > >>>> But it occurred to me that $$ could be a computationally expensive > > >>>> operation to be running every second. > > > > >>> Ummm... did you benchmark it? > > > > >>> -- > > >>> Michael Peters > > >>> Developer > > >>> Plus Three, LP > > > > >-- Jean-Philippe Encausse - Veille / R&D Jalios SA Jp [at] encausse.net - http://www.encausse.com - http://www.jalias.com GTalk: jp.encausse [at] gmail.com - SMS: sms [at] jp.encausse.net Mob: +33 6 82 12 56 99 - Job: +33 1 39 23 92 83 - Tel: +33 1 39 18 90 15 Do it Once, Use it Twice ~ Do it Twice, Make It Once --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---