DEV Community

Cover image for Challenge : Optimizing a little Javascript script...
artydev
artydev

Posted on

Challenge : Optimizing a little Javascript script...

Dear fellows,

For the ones among you who loves doing things in plain Javascript, how would you optimize this code while keeping it very simple ?

I don't mean performance optimizations, only suggestions to avoid memory leaks..

You can test it here : challenge

const state = {
  count: 0
}

const actions = {
  inc: () => {
    state.count += 1;
    view();
  }
}

const setupEvents = () => {
  document
    .querySelector("button#counter")
    .addEventListener("click", actions.inc);
}

const view = () => { 
  dividapp.innerHTML = 
  `
    <h1>Counter ${state.count}</h1>
    <button id="counter">INCR</button>
  `
  setupEvents();
}

view();

Crédits : the image is from BlendSwap

Top comments (1)

Collapse
 
artydev profile image
artydev

Thanks Tony you won :-D
Look at this reken.dev/ it pushed your concept a little further :-)
Regards