javascript - Node.js JSON.parse on object creation vs. with getter property -


this largely 'am doing right / how can better' kind of topic, concrete questions @ end. if have other advice / remarks on text below, if didn't ask questions, feel free comment.


i have mysql table users of app that, along set of fixed columns, has text column containing json config object. store variable configuration data cannot stored in separate columns because has different properties per user. there doesn't need lookup / ordering / on configuration data, decided best way go.

when querying database node.js app (running on node 0.12.4), assign json text object , use object.defineproperty create getter property parses json string data when needed , adds object.

the code looks this:

user =      uid: results[0].uid     _c: results[0].user_config # json config data string  object.defineproperty user, 'config',     get: ->         @c = json.parse @_c if not @c?         return @c 

edit: above code coffeescript, here's (approximate) javascript equivalent of don't use coffeescript:

var user = {     uid: results[0].uid,     _c: results[0].user_config // json config data string };  object.defineproperty(user, 'config', {     get: function() {         if(this.c === undefined){             this.c = json.parse(this._c);         }         return this.c;     } }); 

i implemented way because parsing json blocks node event loop, , config property needed half time (this in middleware function express server) way json parsed when needed. config data can range 5 around 50 different properties organised in couple of nested objects, not huge amount of data still more few lines of json.

additionally, there 3 of these json objects (i showed 1 since they're same, different data in them). each 1 needed in different scenarios of scenarios depend on variables (some of come external sources) @ point of function it's impossible know ones necessary.

so had couple of questions approach hope guys can answer.

  • is there negative performance impact when using object.defineproperty, , if yes, possible negate benefit not parsing json data right away?

  • am correct in assuming not parsing json right away improve performance? we're looking @ continuously high number of requests , need process these , efficiently.

  • right 3 json data sets come 2 different tables joined in sql query. have 1 query per request instead of four. keeping in mind there scenarios none of json data needed, scenarios 3 data sets needed (and of course scenarios inbetween), improvement required json data table, @ point when 1 of data sets needed? avoided because feel waiting 4 separate select queries executed take longer waiting 1 query 2 joined tables.

  • are there other ways approach improve general performance more? (i know, one's bit of subjective question, ideas / suggestions of things should check out welcome). i'm not looking spin off parsing json data separate thread though, because our service runs on cluster of virtualised single-core servers, creating child process increase overall cpu usage, @ high loads have more negative impact on performance.

note: when performance means fast , efficient throughput rates. prefer larger memory footprint on heavier cpu usage.

we should forget small efficiencies, 97% of time: premature optimization root of evil
- donald knuth

what article? time spent in optimizing dubious results instead of focusing on design , clarity.

it's true json.parse blocks event loop, every synchronous call - code execution , not bad thing.

the root concern not blocking, how long blocking. remember strongloop instructor saying 10ms rule of thumb max execution time call in app @ cloud scale. >10ms time start optimizing - apps @ huge scale. each app has define threshold.

so, how execution time lazy init save? this article says takes 1.5s parse 15mb json string - 10,000 b/ms. 3 configs, 50 properties each, 30 bytes/k-v pair = 4500 bytes - half millisecond.

when time came optimize, @ having lazy init mysql call. config needed 50% of time, won't block event loop, , external call db absolutely dwarfs json.parse().

all of say: doing not bad or wrong, if whole app littered these types of dubious optimizations, how impact feature addition , maintenance? biggest problems see revolve around time market, not speed. code complexity increases time market.

q1: is there negative performance impact when using object.defineproperty...

check out this site hint.

q2: *...not parsing json right away improve performance...

imho: inconsequentially

q3: right 3 json data sets come 2 different tables...

the majority db query cost out of process call , network data transport (unless have bad schema or config). data in 1 call right move.

q4: are there other ways approach improve general performance

impossible tell. place start observed behavior, profiler tools identify culprit, code optimization.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -