File size: 1,303 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { getShellRc, getMemInMb } = require( '../lib' );

module.exports = {
	title: 'Node memory',
	group: 'Node.js',
	description:
		'Sets the maximum size of Node.js heap. As memory consumption approaches this limit, Node.js will spend more time on garbage collection in an effort to free unused memory.',
	test: ( { pass, fail, ignore } ) => {
		if ( process.platform !== 'darwin' && process.platform !== 'linux' ) {
			ignore( 'This evaluation only works in OSX or Linux' );
			return;
		}

		if ( ! process.env.NODE_OPTIONS ) {
			fail( 'NODE_OPTIONS is not set' );
			return;
		}

		const match = process.env.NODE_OPTIONS.match( /--max-old-space-size=([0-9]+)/ );
		if ( ! match ) {
			fail( 'max-old-space-size not set' );
			return;
		}

		const currentValue = Number( match[ 1 ] );
		const desiredValue = getMemInMb() * 0.75;
		if ( currentValue < desiredValue ) {
			fail( `Memory set to ${ currentValue } mb, at least ${ desiredValue } mb expected` );
			return;
		}

		pass();
	},
	fix: () => {
		const desiredValue = getMemInMb() * 0.75;
		const shell = getShellRc();
		if ( shell ) {
			return `Add \`export NODE_OPTIONS=--max-old-space-size=${ desiredValue }\` to ${ shell }`;
		}
		return `Set env variable \`NODE_OPTIONS\` with value \`--max-old-space-size=${ desiredValue }\``;
	},
};