A
A
andrei_pro2020-01-23 10:32:01
JavaScript
andrei_pro, 2020-01-23 10:32:01

Vuex optimization?

Hello. I'm making a drag and drop component. Works in tandem with vuex.
On the move event, the plugin sends a drag(x, y) event.
Then I do:

<element @drag="onDrag" />

<script>
onDrag(x, y) {
 this.$store.dispatch('dragElement', {element: this.element, x, y})
}
</script>

Vuex:
const actions = {
  dragElement({commit, state}, {element, x, y}) {
    commit('moveElement', {element, x, y})
}

const mutations = {
  moveElement(state, {element, x, y}) {
    element.x = x
    element.y = y
  }
}

strict mode is off.
You need to change the position immediately, that is, with mouseUp it does not fit.
In the debug, the fps is low (52fps), if you close the debug it works well (60fps), but with 30 elements that way, there is no such smoothness.
5e294c24c8d5c181094900.png
5e294bde3a907920521078.png
How can you optimize?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Athanor, 2020-01-23
@andrei_pro

1. Repositioning an element using absolute positioning is slower than using transform . Try changing left/topto transform: translate.
2. There is a magic property in css will-change , try to include it.
3. You were already advised in the answers to try using throttle , it’s definitely worth doing, clearly about how it works and what is the difference with debounce - here

A
Alex Pospeliri, 2020-01-23
@apospeliri

Why are you committing every @drag change? All you have to do is wait for dragEnd and commit it already.

R
Roman Kitaev, 2020-01-23
@deliro

Use throttle (from lodash or wherever) at 5-10ms. The more - the less load will be on the processor. In any case, you most likely do not need hundreds or thousands of events per second.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question