In most cases, constructor comparison is faster than instanceof and typeof, thus using it as type guard can improve performance. The code below demonstrates the desired behavior:
const el:HTMLElement = document.createElement('canvas');
if (el.constructor === HTMLCanvasElement) {
//inside of this scope, el is parsed as an instance of HTMLCanvasElement
el.getContext('2d');
}
In most cases, constructor comparison is faster than instanceof and typeof, thus using it as type guard can improve performance. The code below demonstrates the desired behavior: