是否有可能从一个NodeJS脚本的评论创build内存问题?
我编写NodeJS库,通常在代码中放入JSDoc注释,然后生成文档。
所以,我的代码如下所示:
/** * Sum * Calculates the sum of two numbers. * * @name Sum * @function * @param {Number} a The first number, * @param {Number} b The second number. * @return {Number} The sum of the two numbers. */ module.exports = function (a, b) { return a + b; };
当从另一个NodeJS脚本需要这个脚本时,上面的注释会被加载到RAM中吗?
那么,大的评论是否会影响记忆呢?
我猜NodeJS脚本被parsing,而不相关的东西(例如注释)不被保存在内存中。 这是真的?
所以,总而言之,这样的评论是否会造成任何内存问题?
例子
将函数string化,也会打印注释:
function foo () { // Hello World comment return 10; } console.log(foo.toString());
输出:
$ node index.js function foo() { // Hello World comment return 10; }
另一个例子是在200万行上生成lorem ipsum
,然后在最后一行console.log(1)
。
所以,这个文件看起来像:
// long lorem ipsum on each line // ... // after 2 million lines console.log(1)
运行上面的脚本我得到:
$ node run.js FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory Aborted (core dumped)
这发生在一个16GB的RAM机器上。
我还比较了一个简单的console.log(1)文件和一个有很多评论的文件的性能:
$ time node with-comments.js 1 real 0m0.178s user 0m0.159s sys 0m0.023s $ time node no-comments.js 1 real 0m0.040s user 0m0.036s sys 0m0.004s
当你的.toString()
代码certificate,所有的注释都保存在内存中作为函数源代码的一部分,在函数之外的节点模块中是模块函数。 您可以在构build步骤中删除注释。
V8将源保存在内存中,因为它是函数最紧凑的表示,AST和其他中间表示根据需要随时创build,然后丢弃。