Expansion of String.prototype shows a 10-fold increase in productivity below the challenge of the function



  • I wanted to expand prototype object. String Some useful methods. It worked, but productivity was amazingly low. Transfer of line to function, why faster x10 times than redefinition of method String.prototypethat does the same thing. To verify that this is really happening, I created a very simple example, a function. count() and method.

    (I experimented and created three different versions of the method.)

     
    

    function count(str, char) {
    var n = 0;
    for (var i = 0; i < str.length; i++) if (str[i] == char) n++;
    return n;
    }

    String.prototype.count = function (char) {
    var n = 0;
    for (var i = 0; i < this.length; i++) if (this[i] == char) n++;
    return n;
    }

    String.prototype.count_reuse = function (char) {
    return count(this, char)
    }

    String.prototype.count_var = function (char) {
    var str = this;
    var n = 0;
    for (var i = 0; i < str.length; i++) if (str[i] == char) n++;
    return n;
    }

    // Here is how I measued speed, using Node.js 6.1.0

    var STR ='0110101110010110100111010011101010101111110001010110010101011101101010101010111111000';
    var REP = 1e3//6;

    console.time('func')
    for (var i = 0; i < REP; i++) count(STR,'1')
    console.timeEnd('func')

    console.time('proto')
    for (var i = 0; i < REP; i++) STR.count('1')
    console.timeEnd('proto')

    console.time('proto-reuse')
    for (var i = 0; i < REP; i++) STR.count_reuse('1')
    console.timeEnd('proto-reuse')

    console.time('proto-var')
    for (var i = 0; i < REP; i++) STR.count_var('1')
    console.timeEnd('proto-var')

    Results:

    func: 705 мс
    proto: 10011 мс
    proto-reuse: 10366 мс
    proto-var: 9703 мс

    As you can see, the difference is substantial.

    The example below shows that the productivity of the challenge of methods is slower and that the function itself is copper.

    var STR ='0110101110010110100111010011101010101111110001010110010101011101101010101010111111000';
    var REP = 1e3//6;

    function count_dummy(str, char) {
    return 1234;
    }

    String.prototype.count_dummy = function (char) {
    return 1234; // Just to prove that accessing the method is not the bottle-neck.
    }

    console.time('func-dummy')
    for (var i = 0; i < REP; i++) count_dummy(STR,'1')
    console.timeEnd('func-dummy')

    console.time('proto-dummy')
    for (var i = 0; i < REP; i++) STR.count_dummy('1')
    console.timeEnd('proto-dummy')

    console.time('func-dummy')
    for (var i = 0; i < REP; i++) count_dummy(STR,'1')
    console.timeEnd('func-dummy')

    Results:

    func-dummy: 0.165 мс
    proto-dummy: 0.247 мс
    func-dummy: 0.045 мс

    Despite the large number of repetitions (some 1e8), prototyped methods have proved to be more than 10 times slow, but this may be ignored.

    This can only be linked to the object String because the same thing is being performed, regardless of the method of causing the methods:

    var A = { count: 1234 };

    function getCount(obj) { return obj.count }

    A.getCount = function() { return this.count }

    console.time('func')
    for (var i = 0; i < 1e9; i++) getCount(A)
    console.timeEnd('func')

    console.time('method')
    for (var i = 0; i < 1e9; i++) A.getCount()
    console.timeEnd('method')

    Results:

    func: 1689.942 мс
    method: 1674.639 мс

    The search for Stackoverflow was inconclusive, except for the general recommendation,Don't expand String and Array-- it's a lot of names."What is not a problem specifically for my project, I couldn't find productivity-related methods than functions. So I should just forget the expansion of String's class due to the fall in the productivity of added methods? Does anyone know more about this?

    Translation of question https://stackoverflow.com/questions/38407760/ ” https://stackoverflow.com/users/1968972/exebook ♪



  • Probably because you're using it. "use strict"and this must be shown in a copy String inside your method, instead of being a primitive line.

    You can check that by repeating your measurements. var STR = new String('01101011…')

    To this end, correct your implementation:

    String.prototype.count = function (char) {
        "use strict";
        var n = 0;
        for (var i = 0; i < this.length; i++)
            if (this[i] == char)
                n++;
        return n;
    };  
    

    Translation of response https://stackoverflow.com/questions/38407760/ ” https://stackoverflow.com/users/1048572/bergi ♪




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2