Tuesday, October 25, 2016

Angular.js or Backbone.js?

Anybody here claiming that one solution is faster or slower than other either doesn't know much about any of these libraries or frameworks (or perf testing in general) or is a liar.
Performance is a very tricky characteristic to measure because of so many variables that affect it. Just to name few:
  • quality of the test/benchmark code
  • quality of the library/framework code
  • type of application
  • quality of the application code
  • browser used
  • client hw
  • other processes running at the same time on the client hw
  • quality and speed of the internet connection
  • server load and server performance
  • and the list goes on and on...
but more importantly, what exactly do you mean by performance? performance is a very broad term that covers too many things, including:
  • time it takes to bootstrap the app
  • time it takes to respond to a user action
  • resource utilization (cpu/memory/network)
  • performance of dom manipulation done by the library/framework/app code
  • garbage collector friendliness
  • and again the list goes on and on...
The best way to answer your question is to create an application that is well representative of the application you are intending to build and implement it with the competing libraries/frameworks. Then write a quality benchark that will compare them head to head in a stable environment.
This is obviously a very laborious task and only someone with a lot at stake would undertake it.
There is however a different solution to this problem: understand the framework/library you are using and specifically:
  • learn the core flows and algorithms that the framework/library internally uses. while you typically shouldn't care, when you get into a perf problems, understanding how your application executes, will enable you to identify and fix perf problems
  • check if performance is something that the library/framework writers have expertise in
  • check if the framework/library helps you to identify performance problems and fix them
As for the actual comparison between Backbone and AngularJS, you are comparing two very different solutions.
Backbone doesn't do any dom manipulation for you, so the speed of your app will mostly depend on how well can you do dom manipulation (is this your expertise?).
AngularJS does most of the dom manipulation for you and we have a ton of expertise in this area, so unless you are really good, you'll have a hard time matching us.
Secondly, backbone's model mutation observation is based on events, model wrappers and use of artificial getters and setters. Not only that this can be very inefficient due to lack of event coalescence (there might be a workaround for this in latest backbone versions), but the use of artificial getters and setters also interferes with the JIT compiler in your browser.
Misko wrote up a long post on how Angular does its magical model mutation observation. So I'm not going to repeat it here. But basically the performance of an AngularJS app is directly related to the number and complexity of bindings used in the current view of the application. With this in mind, you can easily predict Angular's performance. Even better is that with tools like AngularJS Batarang extension for Chrome, we allow you to easily instrument your application and understand which bindings on the page are slow and this allows you to focus on fixing the parts of your code that really matter.
I'm going to conclude by saying that no library or framework will be the best solution for all of your use cases, so you should learn more about the tools you build your apps with and when it really matters, decide which one is the best for a given use case. My bet is that for most of the apps you are going to write, performance is not going to noticeably change if you switch framework or library. So I would put more weight on other factors like productivity, easy of use, testability, community and documentation before I would worry about performance.
And the very last thing: benchmarks are often misleading, but check out these ones and take them with a grain of salt.

No comments:
Write comments
Recommended Posts × +