javascript性能是否受到深度递归的影响? [英] Does javascript performance suffer from deep recursion?

查看:139
本文介绍了javascript性能是否受到深度递归的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个javascript来通过javascript帮助模拟HTML中的WPF DockPanel布局行为。

I've written a javascript to emulate WPF DockPanel Layout behaviour in HTML through the help of javascript.

现在,当我开始将这些面板嵌套到10的递归级别时,我遇到了性能问题。奇怪的是,它只不过是普通的递归而且在最深层次上有问题的函数在0到2毫秒之间完成。

Now i'm running into performance issues once i begin nesting those panels to a recursion level of 10. Oddly enough it's nothing more than ordinary recursion and on the deepest level the function in question is finishing in between 0,2 and 2ms.

现在,我确实有一些鬼性能损失,或者调用javascript的递归是否有巨大的成本?我希望你们其中一个人知道。

Now either i do have some ghost performance loss, or is there a massive cost in invoking recursion for javascript? I hope one of you knows.

如果有成本,显而易见的解决方案是递归展开,这将是相当悲伤的。

If there's a cost the obvious solution would be recursion unrolling which would be rather sad.

我已阅读在JavaScript中调用SO-Recursive函数在此,但这是否真的意味着我可能不得不接受recursiondepth n = functioncost *(10 ^(n-1))我会去的每个递归深度?

I've read SO-Recursive function calling in JavaScript On this, but does that really mean that i may have to accept recursiondepth n = functioncost * (10^(n-1)) for every depth of recursion i'll go?

此外(这反驳了递归比迭代慢的想法) SO - 迭代比递归更快,还是更不容易出现堆栈溢出?

Also this (which refutes the idea of recursion beeing slower than iteration) SO - Is iteration faster than recursion, or just less prone to stack overflows?

这个程序员:性能:Javascript中的递归与迭代,说迭代比递归快4倍(叹息......)

And this Programmers: Performance: recursion vs. iteration in Javascript, which speaks for iteration beeing faster than recursion by a factor of 4 (sigh...)

这是一个普遍的问题,独立于浏览器JS引擎。如果你知道它在一个中速度慢但在另一个中速度很快,那么信息也会受到欢迎。我假设它将是相同的。

This is a general question, independant of browser JS engine. If you know about it beeing slow in one but fast in another that information would be welcome too. I was assuming that it would be the same in all.

访问者的包装信息:递归与迭代的影响非常显着。迭代一般会赢。


  • 因子FF30:5~

  • 因素Chrome 36 :40~

  • 因子Native IE8,WinXP:10~

推荐答案

是的,递归对JavaScript的性能有很大的影响,总是避免它,只使用迭代方法

Yes, the recursion has a very big impact on performance in JavaScript, always avoid it, use only iterative approach

斐波纳契函数的一个简单例子(递归) vs loop):

A simple example of fibonacci function (recursion vs loop):

http ://jsperf.com/fibonacci-recursive-or-iterative/4

我前一段时间写的另一个例子(对象导航):

Another example written by me some time ago (object navigation):

http://jsperf.com/object-navigation

var a = {
    b: {
        c: 'd'
    }
};

find(a, 'b/c'); // => 'd'

OP-Test: http://jsperf.com/iterative-vs-recursive-method-invocation/3

这篇关于javascript性能是否受到深度递归的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆