0

I'm currently working with a personal library that's accumulated quite a number of "helper" functions, which are used throughout my architecture for various purposes. Back when there were only a few of them, I kept them in a single file and object so I could access them like so:

tools.parseSomething(obj);

This has been terribly handy, and keeps the code still somewhat organised and readable. The problem is that the file (and object) containing these methods is growing to an enormous size, and needs a cleanup. I was considering creating separate files for "categories" of functions and placing them in those, so they'd be accessed like:

tools.env.getEnvironmentInfo();

My concern with this approach is not the readability so much, but the performance of the look-up. From what I've read recently, object look-ups are no longer the major bottle-neck they used to be, but I still want my library to be as efficient as possible (readability second).

I also considered having the separate files, but add all of the functions to the parent object so they're still accessible in the original way, but stored separately. The object containing the functions is "static" and exists as a single instance.

My question is, with regards to what I've explained, what would be the most efficient method of storing and using a large amount of helper functions? By efficiency I mean performance, which is the sole area of concern for me at the moment.

16
  • Doesn't exist? I've read that the V8 engine has some nifty functionality behind it to deal with object property look-ups in a smarter way than the typical dictionary-style reference, but I'm not sure that every JS engine does something similar. Surely something.test() is faster than something.inner.more.test() ? Commented Apr 22, 2014 at 7:01
  • While it is true that nested objects are slightly slower, the difference is really negligible at those execution speeds. You'll gain more efficiency by optimizing your functions. Commented Apr 22, 2014 at 7:01
  • @Perry It doesn't exist [for any meaningful metric in most all code that exists]. Do not waste time with such micro-optimizations. If there is code for which it matters, it still "doesn't exist" until there is a performance test-case showing that it does. Without a performance metric there is no way to tell which effect, if any, the additional property access has in the grand scheme of things (e.g. exactly which code in exactly which engine under which JIT/optimizations), nor is there a way to demonstrate changes lead to positive improvements in performance. Commented Apr 22, 2014 at 7:02
  • @Cerbrus I ran that perf test in Chrome and IE9. While in Chrome the difference is very-much negligible, in IE the extra layer is "21% slower". The library is used a lot in IE, but I guess ~23 million calls is somewhat unlikely in my case.. Commented Apr 22, 2014 at 7:06
  • @user2864740: Look at the jsPerf I just posted. There is a clear penalty in execution speed when nesting objects deeper. The difference may be insignificant compared to unoptimized functions, but it's there. Commented Apr 22, 2014 at 7:06

1 Answer 1

2

I'd agree that premature optimization is going on here, however that wasn't your question.

I'd assert that instead of thinking in terms of loosely related scriptlets that 'do kind of related things', I'd suggest an richer object model that can be stateful and perhaps expose the behaviors you need while hiding some optimizations you feel are necessary like caching lookups, etc.

So instead of getEnvironmentInfo, why not have an Environment instance that has, for example, already fetched some parameters and stored internally so subsequent calls are made faster. From there you can create new environments or whatever other behaviors suit you.

This is just good programming practice in whatever language.

Sign up to request clarification or add additional context in comments.

2 Comments

Couldn't have said it better. +1
Thanks for the insight. I merely used getEnvironmentInfo() as an example, as some "once-off" functions do cache their calculated values. I've tried to "class-ify" as much as possible to keep everything neat and to avoid repetitive processing of the same information, but for now there is still some need for the 'library' of functions I currently have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.