Skip to content

Instantly share code, notes, and snippets.

@iNewLegend
Created February 25, 2023 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iNewLegend/f7c41f035cbad93479e5a8f8ade5cc6b to your computer and use it in GitHub Desktop.
Save iNewLegend/f7c41f035cbad93479e5a8f8ade5cc6b to your computer and use it in GitHub Desktop.
Find memory leak in node example.
/**
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
*/
import { Component } from "@internal/core/component";
import * as assert from "assert";
describe( 'Core', () => {
describe( 'Component', () => {
test( 'render() :: should have no memory leaks', async function () {
// Arrange.
class TestComponent extends Component {
static getName() {
return "TestComponent";
}
template() {
return <div>Test Component</div>;
}
}
const component = new TestComponent( document.createElement( 'div' ) );
for ( let i = 0; i < 200; i++ ) {
component.render();
}
const initialMemory = process.memoryUsage();
for ( let i = 0; i < 100; i++ ) {
component.render();
}
// Act - Promise with a timeout to allow the garbage collector to run.
await new Promise( resolve => setTimeout( resolve, 0 ) );
const currentMemory = process.memoryUsage();
// Assert.
assert.ok(
initialMemory.heapUsed >= currentMemory.heapUsed
);
} );
} )
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment