1

Trying to use https://www.npmjs.com/package/xpath

var xpath = require('xpath');

var xpath_select = xpath.useNamespaces({
    "my": "http://my.com/core/api"
});

module.exports.handler = function (request, context, callback) {
    ...
    xpath_select(...)
    ...
};

Should I move xpath_select initialization to handler function or I can initialize it once at startup?

I am aware of multi-threading issues.

Boris
  • 1,054
  • 1
  • 13
  • 23
  • 1
    node is single threaded. There's no thread safety issue – slebetman May 25 '17 at 14:18
  • @slebetman Do you mean that several simultaneous requests will be handled one by one and not in parallel ? – Boris May 25 '17 at 20:25
  • 1
    Yes. In node no code run in parallel since it is single threaded (you can of course use clustering which is multi process or you can use libraries which implement web workers which is multi threaded but most of them use a message passing model where the threads and the main process don't share memory). How node handle parallel requests is that it WAITS in parallel. That's what async IO / nonblocking IO / evented / concurrent programming (it has many names) is all about. There is a saying, concurrent != parallel. – slebetman May 25 '17 at 23:14
  • There used to be actual pthreads libraries written for node (written in C of course) way back in 2012. But I've not seen anyone use them since. Paradoxically with high throughput network servers threading can slow down your program (often caused by locks). The reason node is still popular in 2017 after more than 5 years of criticism by people who thought it's singlethreaded nature was stupid is because it has held up its performance promise. My company for example have switched our Java servers from multithreaded to singlethreaded for the same reason (using play framework) – slebetman May 25 '17 at 23:20
  • See my answer to this question: https://stackoverflow.com/questions/34855352/how-in-general-does-node-js-handle-10-000-concurrent-requests/34857298#34857298 – slebetman May 25 '17 at 23:23

0 Answers0