h5bp / Front-end-Developer-Interview-Questions

A list of helpful front-end related questions you can use to interview potential candidates, test yourself or completely ignore.

Home Page:https://h5bp.org/Front-end-Developer-Interview-Questions/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suggested Q: Memory implications of using closure methods vs prototype methods

mastef opened this issue · comments

Which of these is more memory efficient, and why?

function ProtoPerson() {}

ProtoPerson.prototype.getName = function() {
	return "MyName";
};

ProtoPerson.prototype.getLink = function() {
	return "http://mycompany.com/myLinks/" + this.getName();
};

var protoPersons = [];
for (var i = 0; i < 100000; i++) {
	protoPersons.push(new ProtoPerson);
}
function ClosurePerson() {

	this.getName = function() {
		return "MyName";
	};

	this.getLink = function() {
		return "http://mycompany.com/myLinks/" + this.getName();
	};

}

var closurePersons = [];
for (var i = 0; i < 100000; i++) {
	closurePersons.push(new ClosurePerson);
}

This should just be a PR, although from an editorial perspective, this particular sort of question doesn't fit with what we normally do.