Skip to content

Commit

Permalink
suggestion for performance (#49)
Browse files Browse the repository at this point in the history
just a tiny change to improve memory usage. A weakmap entry get automatically removed when key get garbage collected. which would in this case only happen when you remove a switch. and therefor also no longer need the meta data you store in the map.

I would also suggest to create a custom element that extends the input element, this way you do not have to manually init each switch, and all switches added in runtime are also automatically initialized for you. with the current code if I were to add a new switch to the DOM it would not work with the drag. I do realise this is out of the scope of this challenge. but I estimate it is about the same amount of code, and sets a good example for beginners imo (custom elements / webcomponents are the future imo)
  • Loading branch information
chris-kruining authored Aug 11, 2021
1 parent 3a28c0e commit fd7753b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions switch/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {getStyle, getPseudoStyle} from './style-utils.js'

const elements = document.querySelectorAll('.gui-switch')
const switches = new Map()
const switches = new WeakMap()

const state = {
activethumb: null,
Expand Down Expand Up @@ -116,4 +116,4 @@ window.addEventListener('pointerup', event => {
if (!state.activethumb) return

dragEnd(event)
})
})

0 comments on commit fd7753b

Please sign in to comment.