Prototypes or locks for consistent execution and performance?

What is the best trade-off between executing executives and run-time consistency for calling a method in javascript?

I am still learning javascript and will use prototypes for most of everything (e.g. Brendan Eich here ), but I think I will find better performance and consistency from function closures (I know that it’s probably over optimization). One prototype sample that I tested:

function PrototypeA() {} PrototypeA.prototype.a = 0; PrototypeA.prototype.b = 0; PrototypeA.prototype.someMath = function() { this.a += 1; this.b += 2; }; var Test = new PrototypeA(); Test.someMath(); 

It was determined after reading this (excellent!) Post before the control closures. I am drifting the current closing pattern in the direction:

 var SubModule = new((function() { var a = 0; var b = 0; var someMath = function() { a += 1; b += 2; }; return function() { this.someMath = someMath }; })())(); 

I find that many tests of various patterns are misleading due to errors in the test setup, such as calling constructors in the reference or calling methods in ways that cause different path traversal paths. I'm just trying to check the execution of a method.

After a lot of benchmarking locally (benchmark.js for many different templates), I set up notable templates:

Jsperf here

My data seems to confirm that well-organized closures do not reject at runtime, like so many other types of objects (I use the term β€œtype” freely). I understand that I could be completely mistaken due to any number or errors in the benchmark or setup.

Thanks for watching!

+6
source share
2 answers

Since your use case is an animation, you want to stick with closures. A simple rule of thumb with javascript is "the more . Is, the slower the code goes." In the examples you posted to jsperf, the slowest are the ones that reference this quite a bit. Everyone , when you do this, the engine should look at the current object and determine if what you are trying to access the object is accessible, and if it should look for a prototype chain, On the close side of things, you refer to a rather limited scope predefined by the function which you wrote, and most of all the engine should be able to look at this area in order to refer to the variables in it.

Now, there are some gotcha's. Firstly, using this much faster if you only reference it once or twice. Not in this case, but worth mentioning. The more times you add a link to this , the slower the prototype depends on the code. Take a look at this simple perf for an example. Secondly, if you build a lot of these modules (for example, if you build a data grid), the construction is incredibly slow with closing, and there is a lot of memory lost. See this example for speed differences. The more methods and properties you add, the worse it gets. This is because creating a new closure each time creates unique functions and properties, while the prototype is based only on reusing the same material.

In any case, my humble recommendation is just to look at the situation for which you are programming. In most cases, the use of prototypes is a way of reading (in some cases), reuse, extensibility. But if they are too slow, you simply cannot use them. However, there is nothing wrong with combining the two concepts to give you reusable and modular configuration for methods that do not fall so often. For example, try using "Mixins":

 var Mixin = { interval: 100, finish: function () { window.clearInterval(this.intervalRef); }, start: function (callback) { this.intervalRef = window.setInterval(callback, this.interval) }; var Module = (function () { function getMessage () { return "weee!"; } var somethingThatReallyNeedsSpeed = function () { var iterations, self = this; this.start(function () { console.log(getMessage()); iterations++; if(iterations > 10000) { self.finish(); } }); } somethingThatReallyNeedsSpeed.prototype = Mixin; return somethingThatReallyNeedsSpeed; })(); 

In this example, the build speed does not drop so much because the prototype is only set to reference the Mixin object - there are two methods and a property that a particular function should not create every time, but it is still available through this .

</rant>

+10
source

Searching a chain of chains is usually faster than searching a chain of prototypes: Searching for a chain of regions and finding prototypes in a chain Β· JSPerf

A few points to remember:

  • Never declare a variable in a prototype function. This leads to unexpected problems. Only functions must be declared on prototype .
  • If you want all instances of a function to share a variable, declare it as a property of the function itself.
  • Never write code like new((function() { ... })())() unless you want people to come for you with forks and fire signs.
+3
source

Source: https://habr.com/ru/post/945397/


All Articles