Re: [Wikitech-l] "js2" coding style for html output
[snip] > what I think we have here, is that $('#cat') is expensive, and run > inside a loop in dojBuild > you can build and append in the jquery version and it only shaves 10ms. ie the following still incurs the jquery html building function call costs: function dojBuild(){ var o =''; for(var i =0 ;i < repetCount;i++){ o+=$('') .attr({ 'id': i, 'class':'fish' }) .append( $('') .attr({ 'class':'dog', 'rel' : 'foo' }) .text( i ) ).html() } $('#cat').append(o); } ___ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
Re: [Wikitech-l] "js2" coding style for html output
Semi-on topic for this thread: anyone seen this whacky trick? http://www.stevesouders.com/blog/2009/09/26/mobile-gmail-and-async-script-loading/ http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html - d. ___ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
Re: [Wikitech-l] "js2" coding style for html output
On Mon, Sep 28, 2009 at 6:44 PM, Michael Dale wrote: .. > I think both are useful and I like jquery style building of html since > it gives you direct syntax errors rather than html parse errors which > are not as predictable across browsers. But sometimes performance wise > or from a quick "get it working" perspective its easier to write out an > html string. Also I think tabbed html is a bit easier on the eyes for > someone that has dealt a lot with html. probabbly not the intend of your message, but your first and second examples can be mixed function dojBuild2(){ var box = document.createElement("div"); for(var i =0 ;i < repetCount;i++){ var thing = document.createElement("span"); thing.innerHTML ='' + '' + escape(i) + '' + ''; box.appendChild(thing); } document.getElementById("cat").appendChild(box); } what I think we have here, is that $('#cat') is expensive, and run inside a loop in dojBuild Since your post is about coding style, and not perfomance (and not about the particular speed of this style), ignore this post. -- -- ℱin del ℳensaje. ___ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
[Wikitech-l] "js2" coding style for html output
My attachment did not make it into the JS2 design thread... and that
thread is in summary mode so here is a new post around the html output
question. Which of the following constructions are easier to read and
understand. Is there some tab delimitation format we should use to make
the jquery builder format easier? Are performance considerations
relevant? (email is probably a bad context for comparison since tabs
will get messy and there is no syntax highlighting)
Tim suggested that in security review context "dojBuild" type html
output is more strait forward to review.
I think both are useful and I like jquery style building of html since
it gives you direct syntax errors rather than html parse errors which
are not as predictable across browsers. But sometimes performance wise
or from a quick "get it working" perspective its easier to write out an
html string. Also I think tabbed html is a bit easier on the eyes for
someone that has dealt a lot with html.
Something thats not fun about jquery style is there are many ways to
build that same html string using .wrap or any of other dozen jquery
html manipulation functions ... so the same html could be structured
very differently in the code. Furthermore jquery chain can get pretty
long or be made up of lots of other vars, potentially making it tricky
to rearrange things or identify what html is coming from where.
But perhaps that could be addressed by having jquery html construction
conventions (or a wrapper that mirrored our php side html construction
conventions? )
In general I have used the html output style but did not really think
about it a-priori and I am open to transitioning to more jquery style
output.
here is the html: you can copy and paste this in... on my system Firefox
nightly str builder hovers around 20ms while jquery builder hovers
around 150ms (hard to say what would be a good target number of dom
actions or what is a fair test...) ...jquery could for example output to
a variable instead of direct to dom output shaving 10ms or so and many
other tweaks are possible.
Jquery vs str buider
http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js";>
var repetCount = 200;
function runTest( mode ){
$('#cat').html('');
var t0 = new Date().getTime();
if( mode =='str'){
doStrBuild();
}else{
dojBuild();
}
$('#rtime').html( (new Date().getTime() - t0) + 'ms');
}
function doStrBuild(){
var o = '';
for(var i =0 ;i < repetCount;i++){
o+=''+
'' +
'' +
escape(i) +
'
' +
'';
}
$('#cat').append(o);
}
function dojBuild(){
for(var i =0 ;i < repetCount;i++){
$('')
.attr({
'id': i,
'class':'fish'
})
.append( $('')
.attr({
'class':'dog',
'rel' : 'foo'
})
.text( i )
).appendTo('#cat');
}
}
Jquery vs dom insert
Run Time:
Run Str
Run Jquery
--michael
___
Wikitech-l mailing list
Wikitech-l@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikitech-l