Skip to content

debouncing #7

@shimaore

Description

@shimaore

Context: I'm using S.js/SArray in a Surplus application, with a lot of async updates (via socket.io). I'm trying to par down the number of DOM updates, specifically with code that has to deal with multiple operations on SArray; my bottleneck was in code similar to:

var filtering_field = S.data('hello') // value from UI element
var filtering_function = S( (value) =>
  return value === filtering_field()
)
var original_list = SArray( [] ) // updated over socket.io
var filtered_list = S( =>   // displayed using Surplus
  return original_list.filter(filtering_function).sort().splice(0,99)
)

In this example, filtering_field is the value of a UI element, while original_list is updated over socket.io. The filtered_list is then displayed using Surplus.

I've been debouncing native values using S.js with code that looks like the following (this is more-or-less my coffeescript code with-parentheses-added for clarity):

var debounce = (src,timeout,conclude) =>
  var timer = null
  S( () =>
    var v = src()
    clearTimeout(timer)
    timer = setTimeout( ( () => conclude(v) ), timeout )
    return
  )
  return

To create a debounced signal I then use:

var debounced_signal = S.data( null )
debounce( original_signal, timeout, debounced_signal )

However this is clearly sub-optimal when using SArray:

var debounced_array = SArray( [] )
debounce( original_array, timeout, debounced_array )

because the entire SArray gets overwritten every time, which means that .filter etc. are not optimized on changed values, but are re-applied to the entire Array every time. (Assuming I understand what SArray does!!)

Is there a better way to debounce an SArray?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions