Search code examples
node.jsdomjsoupserver-side

Technology for Server side DOM manipulation (Java vs Node)


I am doing a POC in which I have to manipulate DOM before giving HTML content to client/browser.

Some Use Cases:

  1. Inject a javascript in html page and this javascript is customer specific.
  2. Inject the stylesheet according to the theme chosen by user earlier.
  3. ... More such cases are there with some business logic.

I have finished all use-cases. I have implemented it in Java using Jsoup. I have also done it in Node Js. I have to decide which technology to choose ?

Does anyone know any pros/cons in both techniques ? Please suggest how should I test which of the two is better. Any tools available to test out the memory used/resource usage/time taken etc. Though I have already computed time in both cases.

PS: Please feel free to suggest any other technology for server side DOM manipulation and tools to test out its performance and memory.


Solution

  • Choosing between Java/Node.js depends on the application you are building and people will always come up with benchmarks to show performance on one is better than the other. Most of these suggestions will not suit your use case, so instead consider your application, TCO, time to develop etc as parameters apart from raw performance and take a call.

    Remember that node.js is a single threaded async i/o model vs the multithread sync i/o model of java, and that node can be ridiculously fast out of the box whereas java comes with lot of features that can come in handy later. Again, these are subjective too.

    I would suggest DOM manipulation is something you should avoid serverside and try to offload it to client side javascript or use better templating engines. If it is unavoidable, Node.js has good options - try to use the module cheerio which is faster than heavy weight jsdom.

    Again, using these libraries has its tradeoffs. Cheerio does not have advanced DOM manipulations - it tries to achieve jquery like API on the server while jsdom is a full fledged DOM engine.