admin管理员组

文章数量:1323737

I have two loops, one for each day of the month, other with all events for this month. Let's say I have 100 000 events. I'm looking for a way to remove events from the main events List once they were "consumed".

The code is something like:

const calendarRange = [{initialDate}, {initialDate}, {initialDate}, {initialDate}, ...] // say we have 30 dates, one for each day
const events = fromJS([{initialDate}, {initialDate}, {initialDate}, ...]) // let's say we have 100 000
calendarRange.map((day) => {
  const dayEvents = events.filter((event) => day.get('initialDate').isSame(event.get('initialDate'), 'day')) // we get all events for each day 
  doSomeThingWithDays(dayEvents)
  // how could I subtract `dayEvents` from `events` in a way
  // the next celandarRange iteration we have less events to filter? 
  // the order of the first loop must be preserved (because it's from day 1 to day 3{01}])
}

With lodash I could just do something like:

calendarRange.map((day) => {
  const dayEvents = events.filter((event) => day.get('initialDate').isSame(event.get('initialDate'), 'day')) // we get all events for each day 
  doSomeThingWithDays(dayEvents)
  pullAllWith(events, dayEvents, (a, b) => a === b)
}

How to acplish the same optimization with immutablejs? I'm not really expecting a solution for my way of iterating the list, but for a smart way of reducing the events List in a way it get smaller and smaller..

I have two loops, one for each day of the month, other with all events for this month. Let's say I have 100 000 events. I'm looking for a way to remove events from the main events List once they were "consumed".

The code is something like:

const calendarRange = [{initialDate}, {initialDate}, {initialDate}, {initialDate}, ...] // say we have 30 dates, one for each day
const events = fromJS([{initialDate}, {initialDate}, {initialDate}, ...]) // let's say we have 100 000
calendarRange.map((day) => {
  const dayEvents = events.filter((event) => day.get('initialDate').isSame(event.get('initialDate'), 'day')) // we get all events for each day 
  doSomeThingWithDays(dayEvents)
  // how could I subtract `dayEvents` from `events` in a way
  // the next celandarRange iteration we have less events to filter? 
  // the order of the first loop must be preserved (because it's from day 1 to day 3{01}])
}

With lodash I could just do something like:

calendarRange.map((day) => {
  const dayEvents = events.filter((event) => day.get('initialDate').isSame(event.get('initialDate'), 'day')) // we get all events for each day 
  doSomeThingWithDays(dayEvents)
  pullAllWith(events, dayEvents, (a, b) => a === b)
}

How to acplish the same optimization with immutablejs? I'm not really expecting a solution for my way of iterating the list, but for a smart way of reducing the events List in a way it get smaller and smaller..

Share Improve this question edited Nov 25, 2016 at 15:41 enapupe asked Nov 22, 2016 at 17:44 enapupeenapupe 17.1k3 gold badges33 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4 +50

You can try a Map with events split into bins - based on your example, you bin based on dates - you can lookup a bin, process it as a batch and remove it O(1). Immutable maps are fairly inexpensive, and fare much better than iterating over lists. You can incur the cost of a one time binning, but amortize it over O(1) lookups.

Something like this perhaps:

eventbins = OrderedMap(events.groupBy(evt => evt.get('initialDate').dayOfYear() /* or whatever selector */)) 

function iter(list, bins) { 
 if(list.isEmpty()) 
   return
 day = list.first()
 dayEvents = bins.get(day.dayOfYear())
 doSomeThingWithDays(dayEvents)
 iter(list.shift(), bins.delete(day))
}

iter(rangeOfDays, eventbins)

By remobing already processed elements you are not going to make anything faster. The cost of all filter operations will be halved on average, but constructing the new list in every iteration will cost you some cpu cycles so it is not going to be significantly faster (in a big O sense). Instead, you could build an index, for example an immutable map, based on the initialDate-s, making all the filter calls unnecessary.

const calendarRange = Immutable.Range(0, 10, 2).map(i => Immutable.fromJS({initialDate: i}));

const events = Immutable.Range(0, 20).map(i => Immutable.fromJS({initialDate: i%10, i:i}));


const index = events.groupBy(event => event.get('initialDate'));

calendarRange.forEach(day => {
  const dayEvents = index.get(day.get('initialDate'));
  doSomeThingWithDays(dayEvents);
});

function doSomeThingWithDays(data) {
  console.log(data);
}
<script src="https://cdnjs.cloudflare./ajax/libs/immutable/3.8.1/immutable.js"></script>

本文标签: javascriptimmutablejs filter and mutate (remove) found entriesStack Overflow