Last active
November 7, 2015 17:57
-
-
Save thejohnfreeman/a4882782073ee83e5c3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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