UNPKG

gc-hacks

Version:

Collection of ugly hacks, for JS garbage collector. Use only as a last resort.

35 lines (28 loc) 926 B
'use strict'; // Return a copy of the given value, taking a predictable amount of space. function recreateValue(jsonCompatibleData) { var str = JSON.stringify(jsonCompatibleData); return JSON.parse(exports.forceStringCopy(str)); } exports.recreateValue = recreateValue; function gcCollect() { if(typeof global.gc !== 'function') throw Error('You should expose GC, run Node with "--expose-gc".'); global.gc(); } exports.gcCollect = gcCollect; function forceStringCopy(str) { if (typeof str !== 'string') return str; return (' ' + str).substr(1); } exports.forceStringCopy = forceStringCopy; function recreateReturnObjectAndGcCollect(fn) { return function wrapper() { var str = JSON.stringify(fn.apply(this, arguments)) str = exports.forceStringCopy(str); exports.gcCollect(); return JSON.parse(str); } } exports.recreateReturnObjectAndGcCollect = recreateReturnObjectAndGcCollect;