Skip to content

Instantly share code, notes, and snippets.

@thejohnfreeman
Last active November 7, 2015 17:57
Show Gist options
  • Select an option

  • Save thejohnfreeman/a4882782073ee83e5c3e to your computer and use it in GitHub Desktop.

Select an option

Save thejohnfreeman/a4882782073ee83e5c3e to your computer and use it in GitHub Desktop.
function NotPimpl(x) {
// more than one assignment, "deep" copy
this.first = x.first
this.last = x.last
}
NotPimpl.prototype.fullName = function() {
// direct members, fast
return this.first + this.last
}
function Pimpl(x) {
// one assignment, "shallow" copy
this.x = x
}
Pimpl.prototype.fullName = function() {
// indirect members, slower
return this.x.first + this.x.last
}
x = {
"first": "John",
"last": "Doe"
}
np = new NotPimpl(x) // slower
np.fullName() // fast
p = new Pimpl(x) // fast
p.fullName() // slower
// altogether, the speed differences are peanuts not worth worrying about until you reach millions of objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment