Skip to content
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

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/core/utils/dq-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) {
Copy link
Contributor Author

@straker straker May 7, 2024

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, so options and spec were ignored.

options ??= null;
spec ??= {};

if (!options) {
options = cache.get(CACHE_KEY) ?? {};
}
Expand Down Expand Up @@ -82,7 +86,9 @@ function DqElement(elm, options = null, spec = {}) {
if (!axe._audit.noHtml) {
this.source = this.spec.source ?? getSource(this._element);
}
}

return this;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memoize requires something to be returned otherwise it caches the function as undefined

});

DqElement.prototype = {
/**
Expand Down
15 changes: 10 additions & 5 deletions test/core/utils/node-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 DqElement takes the spec as truth, just passed the functions in as the spec.

ancestry: { get: throws },
xpath: { get: throws }
}
);

assert.doesNotThrow(() => {
nodeSerializer.dqElmToSpec(dqElm, {
Expand Down
Loading