[英]Simple javascript file produces memory error
function flipPairs(input){
var text = '';
for(var i = 0; i < input.length; i = i + 2){
text += input[i + 1] + input[i];
}
return text;
}
console.log(flipPairs('HI'));
I have this in a file called flipper.js When I run我在一个名为 Flipper.js 的文件中有这个当我运行时
node flipper.js
I get this error我收到这个错误
node flipper.js
<--- Last few GCs --->
[20506:0x102aac000] 29029 ms: Scavenge 2024.1 (2031.1) -> 2023.8 (2042.3) MB, 9.6 / 0.0 ms (average mu = 0.272, current mu = 0.259) allocation failure
[20506:0x102aac000] 29049 ms: Scavenge 2031.3 (2042.3) -> 2032.2 (2043.1) MB, 16.5 / 0.0 ms (average mu = 0.272, current mu = 0.259) allocation failure
[20506:0x102aac000] 29069 ms: Scavenge 2032.2 (2043.1) -> 2031.2 (2066.1) MB, 19.8 / 0.0 ms (average mu = 0.272, current mu = 0.259) allocation failure
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 0x100972e59]
Security context: 0x1a3a9d5008d1 <JSObject>
1: flipPairs [0x1a3ac10bfe51] [/Users/aneeshakella/Desktop/tests/flipper.js:~1] [pc=0x113893c44259](this=0x1a3ac10bfe31 <JSGlobal Object>,0x1a3a440d5a21 <String[#69]: check out how interesting this problem is, it's insanely interesting!>)
2: /* anonymous */ [0x1a3ac10bfe91] [/Users/aneeshakella/Desktop/tests/flipper.js:10] [bytecode=0x1a3a440d5ca1 offse...
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 0x101018fd1 node::Abort() (.cold.1) [/usr/local/bin/node]
2: 0x10008632b node::FatalError(char const*, char const*) [/usr/local/bin/node]
3: 0x10008646c node::OnFatalError(char const*, char const*) [/usr/local/bin/node]
4: 0x100187727 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
5: 0x1001876c7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
6: 0x100312e75 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/usr/local/bin/node]
7: 0x1003146ca v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [/usr/local/bin/node]
8: 0x10031114e v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/usr/local/bin/node]
9: 0x10030ef00 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/usr/local/bin/node]
10: 0x10030df51 v8::internal::Heap::HandleGCRequest() [/usr/local/bin/node]
11: 0x1002d36f1 v8::internal::StackGuard::HandleInterrupts() [/usr/local/bin/node]
12: 0x1006389ac v8::internal::Runtime_StackGuard(int, unsigned long*, v8::internal::Isolate*) [/usr/local/bin/node]
13: 0x100972e59 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/usr/local/bin/node]
zsh: abort node flipper.js
It should be a very simple run.这应该是一个非常简单的运行。 Is there a reason why this is failing.
这是否有失败的原因。 My node version is 12.16.3
我的节点版本是 12.16.3
the problem is the length of the input, try this:问题是输入的长度,试试这个:
function flipPairs(input) { var text = ''; for (var i = 0; i < input.length - 1; i = i + 2) { text += input[i + 1] + input[i]; } return text; } console.log(flipPairs('HI'));
You are trying to call an element that is not listed and an undefined value is returned.您正在尝试调用未列出的元素并返回未定义的值。
For example;例如;
let array = [1, 2, 3, 4, 5]
for(let i = 0; i < array.length; i = i + 2){
console.log(array[i + 1])
}
The output here is: 2, 4, undefined这里的 output 是:2、4、未定义
Trying to add an undefined value into the text.试图在文本中添加一个未定义的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.