最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript static methods vs prototypalinstatiated methods on performance - Stack Overflow

programmeradmin6浏览0评论

I have been experimenting using static methods in Javascript. Instead of having objects inherit from a prototype, I use duck-typing a lot harder.

var Controller = {};
Controller.getData = function() {//return data};

// and then in use:
var page = route.getPage();
require([page], function(Controller) {
    Controller.getData();
});

I could do this same by creating new objects with the Controller prototype:

function Controller() {};
Controller.prototype.getData = function() {//return data};

// and then in use:
var page = route.getPage();
require([page], function(Controller) {
    var controller = new Controller();
    controller.getData();
});

My gut feeling is that the static method will be faster, but I have no clue. In general, what are the performance discrepancies between these two methods?

TLDR; basically this question but for Javascript.

I have been experimenting using static methods in Javascript. Instead of having objects inherit from a prototype, I use duck-typing a lot harder.

var Controller = {};
Controller.getData = function() {//return data};

// and then in use:
var page = route.getPage();
require([page], function(Controller) {
    Controller.getData();
});

I could do this same by creating new objects with the Controller prototype:

function Controller() {};
Controller.prototype.getData = function() {//return data};

// and then in use:
var page = route.getPage();
require([page], function(Controller) {
    var controller = new Controller();
    controller.getData();
});

My gut feeling is that the static method will be faster, but I have no clue. In general, what are the performance discrepancies between these two methods?

TLDR; basically this question but for Javascript.

Share Improve this question edited May 23, 2017 at 12:25 CommunityBot 11 silver badge asked May 22, 2015 at 18:09 eatonphileatonphil 13.8k30 gold badges79 silver badges141 bronze badges 6
  • You should put up a test case at jsperf. and test your assumptions (here: jsperf./static-vs-prototype-methods-test, difference looks marginal for the most basic of tests) – Rob M. Commented May 22, 2015 at 18:13
  • I would expect them to perform the same. There might be some weirdness about multiple levels of inheritance, where the prototype chain must be traversed (I'm unsure if JavaScript optimizes lookups). If you are placing something directly on the object you intend to use, I doubt there would be a difference. Honestly, it es down to whether or not getData needs access to an instance of Controller. Like Rob says, give jsperf. a try. It might be worth a test. – Trey Cordova Commented May 22, 2015 at 18:17
  • JavaScript does not support static methods - using prototype is the closest you will get to a static method. The question you have linked is for C# not JavaScript. – Y123 Commented May 22, 2015 at 18:18
  • @YazadKhambata Javascript (ES6) does support static methods (not overly useful unless you are using traceur or babel though) – Rob M. Commented May 22, 2015 at 18:19
  • 1 @YazadKhambata no problem, I remend looking at this post: 2ality./2015/02/es6-classes-final.html – Rob M. Commented May 22, 2015 at 18:21
 |  Show 1 more ment

1 Answer 1

Reset to default 6

Edit: So there is a bit of a performance difference when you are instantiating the class vs. calling the "static" version, but the difference doesn't really warrant you making any changes to your code (premature optimization) unless you are seeing an actual slow down.

As demonstrated in the basic jsperf test I setup, there really isn't much of a difference, performance wise. You should make the decision based on whether or not you want the context (this) to refer to your base class or not.

发布评论

评论列表(0)

  1. 暂无评论