Code Monkey home page Code Monkey logo

Comments (6)

Cuilz avatar Cuilz commented on May 26, 2024 6

看了楼主的分析收获很多,但是undersocre中的_.memorize()方法并不能对提升 fibonacci 方法本身的性能,_.memorize()提升的是多次调用情景下的性能:
var memo_fibonacci = _.memorize(fibonacci);
memo_fibonacci(30);
memo_fibonacci(30); // 这次的调用会使用上一次调用时的cache里存的值,才会有性能的提升;

而楼主帖子内自己写的memorize()应该是优化了fibonacci方法本身,和_.memorize()作用应该是不同的,希望楼主能清晰地说明这一点,容易引起误导,分析的如有不妥,希望能和楼主交流~

from underscore-analysis.

fanyifanbumaimeng avatar fanyifanbumaimeng commented on May 26, 2024 2

es6对尾递归进行了优化,现在也可以直接用尾递归来求解fib了。

from underscore-analysis.

bigggge avatar bigggge commented on May 26, 2024

@Cuilz 楼主的memorize()不也是缓存结果吗?貌似没对fibonacci方法优化,麻烦解释下

from underscore-analysis.

JianShaw avatar JianShaw commented on May 26, 2024

@bigggge
是缓存结果。

优化的话 ,下边的链接应该可以解决你的问题
https://segmentfault.com/a/1190000007115162

from underscore-analysis.

tingtingtinghuang avatar tingtingtinghuang commented on May 26, 2024

更加确切的讲,_.memorize()是通过缓存来实现优化的,只要之后的运算用到了之前缓存下来的结果,那就是有优化了。
_.memorize()提升的是多次调用情景下的性能:
var memo_fibonacci = _.memorize(fibonacci);
memo_fibonacci(30);
memo_fibonacci(n); //纠正一下,上一次的cache其实缓存了[0,30]的值,不单单是n=30的值。
所以n等于其他数值的情况下也有优化。

from underscore-analysis.

lgy87 avatar lgy87 commented on May 26, 2024

@fanyifanbumaimeng
TCO 主要是解决 stackoverflow 的问题的,递归太深的话栈会溢出。
它和 memoize 函数解决的可不是一回事哟。

个人愚见哈。请随时修正我。

from underscore-analysis.

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.