Skip to content

Commit a521064

Browse files
akxdominikgbenmccanndummdidummRich-Harris
authored
fix(perf): use classList.toggle instead of add/remove (#8629)
`classList.toggle(..., flag)` has been a part of the DOM standard forever, so better use it instead of possibly causing browser deopts by using dynamic attribute access. The `!!` is required because an `undefined` flag means flipping the current state. --------- Co-authored-by: Dominik G <[email protected]> Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Simon H <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 734cc19 commit a521064

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* **breaking** Error on falsy values instead of stores passed to `derived` ([#7947](https://github.com/sveltejs/svelte/pull/7947))
1717
* **breaking** Custom store implementers now need to pass an `update` function additionally to the `set` function ([#6750](https://github.com/sveltejs/svelte/pull/6750))
1818
* **breaking** Change order in which preprocessors are applied ([#8618](https://github.com/sveltejs/svelte/pull/8618))
19+
* **breaking** The runtime now makes use of `classList.toggle(name, boolean)` which does not work in very old browsers ([#8629](https://github.com/sveltejs/svelte/pull/8629))
1920
* **breaking** apply `inert` to outroing elements ([#8627](https://github.com/sveltejs/svelte/pull/8627))
2021
* Add a way to modify attributes for script/style preprocessors ([#8618](https://github.com/sveltejs/svelte/pull/8618))
2122
* Improve hydration speed by adding `data-svelte-h` attribute to detect unchanged HTML elements ([#7426](https://github.com/sveltejs/svelte/pull/7426))

src/runtime/internal/dom.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,8 @@ export { ResizeObserverSingleton };
993993
/**
994994
* @returns {void} */
995995
export function toggle_class(element, name, toggle) {
996-
element.classList[toggle ? 'add' : 'remove'](name);
996+
// The `!!` is required because an `undefined` flag means flipping the current state.
997+
element.classList.toggle(name, !!toggle);
997998
}
998999

9991000
/**

0 commit comments

Comments
 (0)