google / google/closure-compiler

performance problem with instanceof, goog.module, Firefox

Open
#2,800 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

After changing my classes to use goog.module (instead of goog.provide and goog.scope) a performance test was 3 times slower under Firefox browser on MacOS (no change in Chrome and Safari browsers). The run-time of the test jumped from 0.92 seconds to 3.01 seconds.

I tracked the problem down to one particular change, and the culprit is an `instanceof` test in a method called [intersectionPossible](https://github.com/myphysicslab/myphysicslab/blob/9e9ce3c5ad2006565d20fcbee76ff679ccd228a7/src/lab/engine2D/StraightEdge.js#L433) which is called 15171840 times during this particular test.

/** @override */
intersectionPossible(edge, swellage) {
if (edge instanceof StraightEdge) {
// Because straight/straight edges never interact (instead they only interact with
// Vertexes) we can avoid some testing and get a performance gain by returning false
// if the other edge is also a straight edge.
return false;
} else {
return super.intersectionPossible(edge, swellage);
}
};

By changing the test to look for a property instead, the performance is back to where it was before.

/** @override */
intersectionPossible(edge, swellage) {
if (edge.isStraightEdge) {
// Because straight/straight edges never interact (instead they only interact with
// Vertexes) we can avoid some testing and get a performance gain by returning false
// if the other edge is also a straight edge.
return false;
} else {
return super.intersectionPossible(edge, swellage);
}
};

/** This function is used to avoid an `instanceof` test, because those are slow
* on Firefox.
* @return {undefined}
*/
isStraightEdge() {};

I suppose this is a bug with Firefox, but I don't know enough about what changes `goog.module` makes to be able to report it to Firefox/Mozilla.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.