-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why use Object.create in exercise 19? #115
Comments
I'm no expert, but I figured from reading it that they were addressing mutable vs. immutable data constructs. They even admit in the explanation paragraph (currently anyway) that it is slower than mutating the object:
Edit: You can see a slightly more real world example of Object.create in exercise 37:
They call the variable |
As @bill-mybiz said, I'm not expert but I think he's right: mutating the original copy through out the call stack would be considered a "side effect" and not very "functional". Creating and returning a new copy would preserve state of the input of each call while still giving you the correctly mutated copy on return. |
Hmm, I understand I was curious as to how a typical immutable JS library would perform, check it out Would love to see even better perf |
Aside from performance, it makes testing a pain, because Jasmine won't look at the prototype properties. |
@gabrielkunkel In that case, would Object.assign be a better alternative and it turns out that Object.assign is faster than Object.create at least according to the below test which is edited from test from @prashcr |
would both create and assign achieve the desired effect? |
@varungupta85 Yes! Aside from being a little bit faster than Object.create(), Object.assign() is more testable with Jasmine. (Solves the issue I ran into while going through the tutorial.) @morenoh149 They do different things, but for the sake of the tutorial they do achieve the same desired effect. See my pull request. |
In the exercise, Object.create is used to "create a fast copy of the accumulated map".
I don't see what's fast or useful about using Object.create instead of simply mutating the map.
I ran tests, and I see no reason why Object.create should be used instead.
http://jsperf.com/object-create-vs-mutation-in-reduce/2
The text was updated successfully, but these errors were encountered: