-
Notifications
You must be signed in to change notification settings - Fork 781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: memoize DqElement #4452
perf: memoize DqElement #4452
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import getXpath from './get-xpath'; | |
import getNodeFromTree from './get-node-from-tree'; | ||
import AbstractVirtualNode from '../base/virtual-node/abstract-virtual-node'; | ||
import cache from '../base/cache'; | ||
import memoize from './memoize'; | ||
|
||
const CACHE_KEY = 'DqElm.RunOptions'; | ||
|
||
|
@@ -36,7 +37,10 @@ function getSource(element) { | |
* @param {Object} options Propagated from axe.run/etc | ||
* @param {Object} spec Properties to use in place of the element when instantiated on Elements from other frames | ||
*/ | ||
function DqElement(elm, options = null, spec = {}) { | ||
const DqElement = memoize(function DqElement(elm, options, spec) { | ||
options ??= null; | ||
spec ??= {}; | ||
|
||
if (!options) { | ||
options = cache.get(CACHE_KEY) ?? {}; | ||
} | ||
|
@@ -82,7 +86,9 @@ function DqElement(elm, options = null, spec = {}) { | |
if (!axe._audit.noHtml) { | ||
this.source = this.spec.source ?? getSource(this._element); | ||
} | ||
} | ||
|
||
return this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Memoize requires something to be returned otherwise it caches the function as |
||
}); | ||
|
||
DqElement.prototype = { | ||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,14 +91,19 @@ describe('nodeSerializer', () => { | |
}); | ||
|
||
it('skips computing props turned off with runOptions', () => { | ||
const dqElm = new DqElement(fixture); | ||
|
||
const throws = () => { | ||
throw new Error('Should not be called'); | ||
}; | ||
Object.defineProperty(dqElm, 'selector', { get: throws }); | ||
Object.defineProperty(dqElm, 'ancestry', { get: throws }); | ||
Object.defineProperty(dqElm, 'xpath', { get: throws }); | ||
|
||
const dqElm = new DqElement( | ||
fixture, | ||
{}, | ||
{ | ||
selector: { get: throws }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With memoize this style of define would propagate to all tests and cause issues. Since |
||
ancestry: { get: throws }, | ||
xpath: { get: throws } | ||
} | ||
); | ||
|
||
assert.doesNotThrow(() => { | ||
nodeSerializer.dqElmToSpec(dqElm, { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default parameters get transformed out of the function arguments after babel, which meant that only
elm
was left in the argument list for memoize, sooptions
andspec
were ignored.