[Prototype-core] iterable/$A
for copying arrays and/or converting iterables like 'arguments' to arrays, // arguments - array newArray = Array.slice( arguments ); // copy a-b copy = Array.slice( original ); --- provides a small increase in speed for a variety of array operations contained in prototype also for array iteration: // declare var outside of loop -- slightly faster... var i; for( i=10; i--; ){ // execute code here } --- in cases where order is not relevant, this method benchmarks faster than any other in all browsers tested ( *safari/mac not tested ) also, for generating unique arrays: function uniqueArray( array ){ var uniqueHash = {}, unique=[], i; for( i=array.length; i--; ){ if( !uniqueHash[ a[i] ]){ unique.push( array[i] ); uniqueHash[ a[i] ] = true; } } return unique; } and is there any reason not to use String.replace([regexp], function() {} ) for a large part of the functions implemented by iterating through arrays? this can be blazingly fast even for complicated regex as long as they are precompiled, and the code is far more readable and compact (i.e. camelization, templating, etc etc ) there are some other much more significant optimizations which might be worthwhile and i would appreciate feedback once i clean up the code a bit more... and --- a big kudos to the prototype team -- discovering prototype way back when completely changed the way i code javascript - thanks. ss --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: iterable/$A
On 8/31/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: newArray = Array.slice( arguments ); Yeah, this is known to us for some time. I've spent some time getting rid of all the $A calls internal to Prototype (they're everywhere!) and replacing them with Array.prototype.slice calls, but I never get around of finishing that. I will, though, before 1.6.0 final. The thing is, I can't suggest this change to the Prototype team unless I actually benchmark the performance. also, for generating unique arrays: function uniqueArray( array ){ var uniqueHash = {}, unique=[], i; for( i=array.length; i--; ){ if( !uniqueHash[ a[i] ]){ unique.push( array[i] ); uniqueHash[ a[i] ] = true; } } return unique; } This is a very nice hack! I'll benchmark that also. and is there any reason not to use String.replace([regexp], function() {} ) for a large part of the functions implemented by iterating through arrays? this can be blazingly fast even for complicated regex as long as they are precompiled, and the code is far more readable and compact (i.e. camelization, templating, etc etc ) Nothing prevents you from submitting patches. We will welcome optimizations for camelization, templating and related stuff. and --- a big kudos to the prototype team -- discovering prototype way back when completely changed the way i code javascript - thanks. Discovering Prototype taught me JavaScript, also. Probably because it didn't have any documentation back then :) Thanks for the suggestions. As I've already said: focus on the string stuff and see if you can get a performance boost. Camelization, for instance, was a bottleneck in setStyle :( --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: iterable/$A
On Aug 30, 2007, at 5:52 PM, Mislav Marohnić wrote: On 8/31/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: also, for generating unique arrays: function uniqueArray( array ){ var uniqueHash = {}, unique=[], i; for( i=array.length; i--; ){ if( !uniqueHash[ a[i] ]){ unique.push( array[i] ); uniqueHash[ a[i] ] = true; } } return unique; } Although I expect this to be faster (linear time), this produces incorrect results for certain arrays due to the implicit toString(). Arrays of DOM elements might not be properly uniq-ed, for example. The same is true for the following array (which uniq() handles properly): var a = [true, true]; console.log( a.uniq() ); // [true, true] console.log( uniqueArray(a) ); // [true] TAG --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: iterable/$A
On 8/31/07, Nicolás Sanguinetti [EMAIL PROTECTED] wrote: As discussed in http://redhanded.hobix.com/inspect/showingPerfectTime.html String.prototype.replace with a callback is broken in Safari (at least up until v2). Ah yes, the Safari issue. TAG, nice catch. I would have never thought of that just by looking at it. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: simulated DOMContentReady event
Dan Webb's LowPro[1] solves this kind of issue by registering a global Ajax.Responder that evaluates DOMContentReady behaviors onComplete. Thinking along similar lines, you could wrap Event.observe so that observers set for contentloaded are also stored into a queue that gets processed after every Ajax.onComplete. I'm just spilling my thoughts here, but I think it would work well for most cases. How do you feel about this? (and, does it have any major drawbacks I'm not seeing?) Best, -Nicolas [1]: http://svn.danwebb.net/external/lowpro/trunk/ On 8/30/07, Jeff Watkins [EMAIL PROTECTED] wrote: Having prototype simulate the DOMContentReady event for those browsers that don't support it is wonderful, however, what is the plan for calling late registrants? For example, imagine you have a widget which would like to perform its initialisation only after the DOM is ready. It registers a listener for contentready on the document. All is fine. Now let's add Ajax to the mix. The widget is instantiated via content injected via Ajax. The registration for contentready will never be called. The solution I've been using is based on someone's Event.onDOMReady code: if the event has already been fired, I call the listener after a brief delay (to maintain the implicit contract call this function later) via window.setTimeout(fn, 0). Am I missing something critical about the implementation of the synthesised contentready event? On the other hand, maybe contentready isn't the right answer here, because when browsers _really_ support DOMContentReady, it won't do what I want anyway. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: iterable/$A
Not only that, but if the array contains objects, the toString() method on the Object prototype will cause all but one object to be removed from the array, because all objects without an overridden toString() method report [object]. On Aug 30, 2007, at 5:50 PM, Tom Gregory wrote: On Aug 30, 2007, at 5:52 PM, Mislav Marohnić wrote: On 8/31/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: also, for generating unique arrays: function uniqueArray( array ){ var uniqueHash = {}, unique=[], i; for( i=array.length; i--; ){ if( !uniqueHash[ a[i] ]){ unique.push( array[i] ); uniqueHash[ a[i] ] = true; } } return unique; } Although I expect this to be faster (linear time), this produces incorrect results for certain arrays due to the implicit toString (). Arrays of DOM elements might not be properly uniq-ed, for example. The same is true for the following array (which uniq() handles properly): var a = [true, true]; console.log( a.uniq() ); // [true, true] console.log( uniqueArray(a) ); // [true] TAG --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---