3

My application does massive rewriting of the DOM on the client side at load time. It traverses the page scanning for special markup (think Markdown) or other patterns, replacing them with sometimes rather complicated DOM structures (using DOM calls such as createElement) to style text but also create diagrams and graphics.

I adopted this architecture in order to avoid any build or preprocessing steps. It works fine in a desktop browser, but is noticeably slow on mobile devices (several seconds, even after relentless optimization). So I would like to rearchitect the system to pre-scan the page and pre-build the DOM. I'm having a bit of a mental block figuring out how to do this. Obviously, I would prefer not to rewrite all the Javascript in some other server-side language. Also, I would like to preserve the option to do the building at load time as I do now, with the basic rewriting logic sharing the same code.

The most likely-sounding option is to build this as a node app, although I am a node beginner. using jsdom both to parse the input and modify the DOM. Or, since I am an a fan of XSLT, and intrigued by Saxon-CE, even though it would mean rewriting everything, I also considered implementing the scanning/rewriting logic in XSLT, to be invoked either from node (for the pre-building case--do people use Saxon from node?) or the browser (for the load-time building case).

Can anyone comment on this approach or throw out alternative ideas?

2 Answers2

3

Not sure what specific use cases you are tackling with the massive DOM rewriting, nor am I sure what your throughput requirements are. That said, one alternate approach to the node/jsdom route could be to run a farm of headless Webkit browsers, and run your current JavaScript as-is in that live rendering context. That would allow you to offload processing from those pokey mobile CPU's into arbitrarily scalable cloud resources (assuming this might be affordable for your project/business), and skirt the need to rewrite or tweak your current, working code at all.

manzoid
  • 1,215
  • 8
  • 6
  • Brilliant. I adapted this solution slightly to meet my needs, simply running PhantomJS locally and capturing the DOM with `var dom = page.evaluate(function() {return document.documentElement.outerHTML;}); console.log(dom);` –  Oct 02 '12 at 11:31
0

Sounds like you want Node. If you already know JavaScript it really is a cinch to pick up.

I would recommend a tutorial like this one: http://www.nodebeginner.org/

It will take you an hour-ish to get through but gives a solid overview of Node as you build a small but functional app along with the author.

Ryan O'Neill
  • 1,710
  • 2
  • 13
  • 24