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 all commits
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
Loading
Loading