Giter VIP home page Giter VIP logo

Comments (7)

 avatar commented on May 12, 2024

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:

// Object.create() is perfect for functional programming because it
// makes creating a new object with a different member value almost
// as cheap as changing the member on the original object!

Edit: You can see a slightly more real world example of Object.create in exercise 37:

function(queue, movieListsMessage) {
  var copyOfMovieLists = Object.create(movieListsMessage.list);
  if (queue !== undefined) {
    copyOfMovieLists.push(queue);
  }
  return copyOfMovieLists;
});

They call the variable copyOfMovieLists, so it looks like it's a nifty way to do an immutable copy of a list.

from learnrx.

kurzninja avatar kurzninja commented on May 12, 2024

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.

from learnrx.

prashcr avatar prashcr commented on May 12, 2024

Hmm, I understand
90% speed penalty is definitely too much for real world use though.

I was curious as to how a typical immutable JS library would perform, check it out
http://jsperf.com/object-create-vs-mutation-in-reduce/4
50% penalty this time.

Would love to see even better perf

from learnrx.

gabrielkunkel avatar gabrielkunkel commented on May 12, 2024

Aside from performance, it makes testing a pain, because Jasmine won't look at the prototype properties.

from learnrx.

varungupta85 avatar varungupta85 commented on May 12, 2024

@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
http://jsperf.com/object-create-vs-mutation-in-reduce/6

from learnrx.

morenoh149 avatar morenoh149 commented on May 12, 2024

would both create and assign achieve the desired effect?

from learnrx.

gabrielkunkel avatar gabrielkunkel commented on May 12, 2024

@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.

from learnrx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.