Skip to content

Commit

Permalink
refactor: get rid of slow iterator-based Array.from
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Sep 2, 2023
1 parent 386d596 commit 20e743b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/common/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ function bindKeys() {
* Ref: https://stackoverflow.com/a/11713537/4238335
*/
export function handleTabNavigation(dir) {
const els = Array.from(document.querySelectorAll('[tabindex="0"],a[href],button,input,select,textarea'))
.filter(el => {
const els = document.querySelectorAll('[tabindex="0"],a[href],button,input,select,textarea')
::[].filter(el => {
if (el.tabIndex < 0) return false;
const rect = el.getBoundingClientRect();
return rect.width > 0 && rect.height > 0;
Expand Down
6 changes: 3 additions & 3 deletions src/common/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ export function forEachValue(func, thisObj) {

export function deepCopy(src) {
if (!src || typeof src !== 'object') return src;
/* Not using `map` because its result belongs to the `window` of the source,
* so it becomes "dead object" in Firefox after GC collects it. */
if (Array.isArray(src)) return Array.from(src, deepCopy);
// Using a literal [] instead of `src.map(deepCopy)` to avoid src `window` leaking.
// Using `concat` instead of `for` loop to preserve holes in the array.
if (Array.isArray(src)) return [].concat(src).map(deepCopy);
return src::mapEntry(deepCopy);
}

Expand Down
4 changes: 2 additions & 2 deletions src/popup/views/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ export default {
},
navigate(dir) {
const { activeElement } = document;
const items = Array.from(this.$el.querySelectorAll('[tabindex="0"]'))
.map(el => ({
const items = this.$el.querySelectorAll('[tabindex="0"]')
::[].map(el => ({
el,
rect: el.getBoundingClientRect(),
}))
Expand Down

0 comments on commit 20e743b

Please sign in to comment.