This improves performance quite a lot because it avoids a huge number of
calls from C++ to JS. Check the ticket for performance measurements.
Refs #2913
This was SVN commit r16337.
This first commit only refactors code and does not add any new feature.
Refs #2913 (see this ticket for more information about the change)
This was SVN commit r15925.