Hacker News new | ask | show | jobs
by esmevane 5115 days ago
This is the pattern I go with: https://gist.github.com/2931439

It decouples a little more logic from the template, and I think it adheres very closely with some things Backbone expects (like passing through the entire el to the DOM).

2 comments

This is pretty much what we use, here it is with map and builds the element prior to dom insertion (better performance): https://gist.github.com/2932520
Well, I'm pretty much going to use this approach from now on. Much cleaner and concise. If you look at the gist history you can see me updating my old code to include fat arrow, and other intuitive-with-experience tricks that I didn't know when I originally drafted the parent code in February.

I'm curious - where did you hear that it was better performance? Or is this personal experience?

Can't remember where I heard it, so decided to check.

Found this S.O thread http://stackoverflow.com/questions/10296791/backbone-js-perf... (different code, same concept) and decided to benchmark: Unqueued DOM insertion: http://jsfiddle.net/arkxp/9/ Queued DOM insertion: http://jsfiddle.net/arkxp/8/

The latter is consistently faster, and while the difference isn't a lot with this example, given a more complex insertion it'll mount up.

That is awesome. Thanks for digging it up

There's at least two projects I have going at the day job which have been experiencing slowdown - and I had no idea that small-batch DOM interaction was the culprit.

Looks like I've got some optimizin' to do.

Yes. This is the way to go. I posted pretty much the same thing, but of course it's half as much code in CoffeeScript... time to give that a serious look.
This is unrelated to the discussion at hand, but: I'd been writing JS for a while, but it wasn't until I picked up CoffeeScript that I really grasped JS.

I know that sounds strange, but CoffeeScript taught me important concepts in JS which I just couldn't grasp beforehand. Prototypal inheritance, scoping, object construction, and use, etc.

I'm a pretty big advocate at this point. Contrary to what a lot of its detractors say, CoffeeScript insinuated in my mind most of the JS best practices. Going back to JS from CoffeeScript, I found JS just as easy to correctly write and use as CoffeeScript had ever been.

How about that?