调用堆栈超出,甚至没有任何递归.这是怎么回事
Callstack exceeded without even any recursion.. what's going on?
>有人知道为什么这可能会导致调用堆栈吗?我什至没有递归。
错误:
C:'Documents and Settings'j'Desktop'bmrepl'STEAL'dedup.js:7
this.push.apply(this, rest);
^
RangeError: Maximum call stack size exceeded
at Array.pluck (C:'Documents and Settings'j'Desktop'bmrepl'STEAL'dedup.js:7:13)
at eatDupe (C:'Documents and Settings'j'Desktop'bmrepl'STEAL'dedup.js:46:15)
at common (C:'Documents and Settings'j'Desktop'bmrepl'STEAL'dedup.js:39:20)
at Object.<anonymous> (C:'Documents and Settings'j'Desktop'bmrepl'STEAL'dedup.js:51:3)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
并且,dedup.js
:
var input = require('prompt'), sugar = require('sugar');
Array.prototype.pluck = function(i) {
var el = this[i];
var rest = this.slice(i + 1 || this.length);
this.length = i < 0 ? this.length + i : i;
this.push.apply(this, rest);
return el;
};
var s = require('fs').readFileSync('NLD_EXSORT.json', {encoding: 'utf8'});
var arr = JSON.parse(s);
if ( arr.filter(function(el){ return el.filter(function(el){ return typeof el != 'string'; }).length != 0; }).length !== 0 ) throw 'nested arrays?'
console.log('all good');
// input.start();
var unq = [];
(function(){
var dupeDate;
for(var i = 0; i < arr.length; i++) {
unq.push(arr[i]);
while(dupeDate = eatDupe())
arr[i].push(dupeDate)
}
function eatDupe(){
if (!arr[i+1]) return false;
if (cmp(arr[i], arr[i+1]) > 2)
return arr.pluck(i+1).pop();
else
return false;
}
})();
console.log(unq.length);
function cmp(a, b){
var common = 0;
while(common < a.length || common < b.length){
if(a[common] == b[common])
common++;
else
return common;
}
if ( a.length == b.length ) return a.length;
else throw 'CMP_ERR';
}
所以抱怨它的代码太多了,但我可以进一步修剪它,我已经这样做了。所以Lorem ipsum dolor也许这是没有必要的,idk。
我的猜测是,当使用多个参数调用时,[].push()
在内部以某种方式递归。很容易确认:
var arr = [];
arr.push.apply(arr, new Array(130000));
// RangeError: Maximum call stack size exceeded
为了避免此错误,我建议您迭代推送它而不是this.push.apply
:
Array.prototype.pluck = function(i) {
var el = this[i];
var rest = this.slice(i + 1 || this.length);
var k, l;
this.length = i < 0 ? this.length + i : i;
this.push.apply(this, rest);
for(k = 0 l = rest.length; k < l; k++) {
this.push(rest[k]);
}
return el;
};
此外,我认为this.splice(i, 1)
等同于整个功能(尽管我不完全确定您要做什么)。
相关文章:
- 数组在递归方法中设置为null
- Kendo:我该如何在树视图中创建一个递归的hieiarchy
- 递归使用 eval() 是检查程序执行的好方法吗?
- 使用递归、Ramda.js和无点样式重构字符串的getPermutations()
- 递归深度比较
- Eloquent JavaScript递归示例如何终止为返回1,但仍然输出指数值
- 递归函数中断
- 如何递归地获取嵌套对象中所有子对象的列表
- JavaScript 素数搜索无限递归
- 在递归生成器函数中,yield后面的*(星号/星号)语法意味着什么
- 递归|两个函数名
- 有没有一种方法可以在Javascript中进行可变递归currying
- 递归Javascript对象是否会导致任何问题(内存泄漏)
- 调用堆栈超出,甚至没有任何递归.这是怎么回事
- 我的递归函数没有返回任何内容
- 匿名递归-任何替换javascript'arguments.calleyee'到其他符号,例如'
- 如何递归地创建指向docpad中特定文件夹中任何文件的链接树
- 函数正在调用自身,而没有在其中实现任何递归调用
- 在数组中将数字相加,没有任何循环.我使用递归解决了.有没有更好的方法
- 如何从JSON创建树没有递归和任何库javascript