JSFiddle - React, Tailwind, and code Playground

by tonyleeper

JavaScript

/**
    prototype vs instance member memory test
*/

var X = function() {};
X.prototype.message = function() { 
    return "Hello world!"; 
};
X.prototype.add = function(a, b) { 
    return a + b;
};
X.prototype.subtract = function(a, b) { 
    return a - b;
};
X.prototype.divide = function(a, b) { 
    return a / b;
};
X.prototype.multiply = function(a, b) { 
    return a * b;
};

var Y = function() {
    this.message = function() { 
        return "Hello world!"; 
    };
    
    this.add = function(a, b) { 
        return a + b;
    };
    
    this.subtract = function(a, b) { 
        return a - b;
    };
    
    this.divide = function(a, b) { 
        return a / b;
    };
    
    this.multiply = function(a, b) { 
        return a * b;
    };
};

var objectsX = [];
console.time("X");
for (i = 0; i < 5000000; i++) {
    objectsX.push(new X());
}
console.timeEnd("X");

/* this will crash the Chrome browser
var objectsY = [];
console.time("Y");
for (i = 0; i < 5000000; i++) {
    objectsY.push(new Y());
}
console.timeEnd("Y");
*/