pouya-eghbali / meteor-autoform

Meteor AutoForm fork, maintained, with tons of performance issues fixed
MIT License
9 stars 1 forks source link

Remove underscore #2

Closed jankapunkt closed 4 years ago

jankapunkt commented 4 years ago

As discussed in the community packages forums, we should remove these utility package dependency.

pouya-eghbali commented 4 years ago

Task list:

[1] throttle function

function throttle(fn, limit) {
  let timeout
  return function (...args) {
    clearTimeout(timeout)
    timeout = setTimeout(function () {
      fn(...args)
    }, limit)
  }
}

[2] once implementation

const fnOnce = () => {
  let alreadyRan = false
  return d => {
    if (alreadyRan) return
    alreadyRan = true
    return fn(d)
  }
}

[3] isObject

export const isObject = obj => toString.call(obj) == '[object Object]' || Array.isArray(obj) || isFunction(obj)

[4] isFunction

export const isFunction = obj => toString.call(obj) == '[object Function]'
jankapunkt commented 4 years ago

I could implement some of them. Do you need support on any of them?

pouya-eghbali commented 4 years ago

Some of them have an obvious alternative, some of them don't. For example I don't know what's the best way to do _.isFunction, should I do x instanceof Function? Should I do "function" == typeof x? For _.isObject it's even more complicated. Should we replace them by the exact underscore definitions?

jankapunkt commented 4 years ago

I think it depends on how "complete" our cases need these Functions to be. I have a gist with very close completeness: https://gist.github.com/jankapunkt/e43405ab19fd939ff058be1e9d4c177e

But this may be total overkill for many cases. Often Objects that are not a subclass of Object (like Array, String etc.) have the constructor [Object] while Array for example has the constructor name [object Array] (which is used via Array.isArray) so for simple checks this may be sufficient for 99% of the cases.

Using instanceof traverses the whole prototype chain and slows down things a lot. Maybe the most performant appraoch is to define the use cases, write some tests with most (better: all) occurring inputs and find the most minimal solution (like using 'function' === typeof x`)

pouya-eghbali commented 4 years ago

@jankapunkt https://gist.github.com/jankapunkt/e43405ab19fd939ff058be1e9d4c177e breaks autoform.

pouya-eghbali commented 4 years ago

I guess that's all of them!

pouya-eghbali commented 4 years ago

Ok, we don't depend on underscore anymore.

pouya-eghbali commented 4 years ago

Going to close this, this is done and if it causes any issues it should be followed in a new thread.