Take the 2-minute tour ×

I want to create a graph based on the combined value of several overlapping events. I want one data point for each change in combined value, so for a set of events like this:

^  ^  ^ ^ ^      ^      ^            ^      ^     

(The number indicates the value so for 3333333 the value is 3, the x axis represent time, the ^ markers represents the changes timestamp result are to be generated for)

I want an output like this:

timestamp0: 0,timestamp1: 2,timestamp2: (2+1),timestamp3:1,

Is it a common way to do this, or a data structure or algorithm typically used to solve these types of problems?

I can manage to do this, but I wonder if there already exists a better way. New events will be added dynamically, and performance of result generation is important.

addition after the first answers arrived:

My current implementation is to loop through a list of events and adding the current one to a set of active events and check if any active events has become invalid since last iteration (these can be generated timestamps for and removed from the active list). The current implementation has a lot of looping and some special case handling (like if two events stop at the same time), so I wonder if you know of a smarter way?

share|improve this question
does 22222 mean five successive instances of event two, or that event two has a duration of five time units? –  Eduardo Mar 2 '12 at 14:15
it means duration of 5 time units, sorry for the confusion –  user1245172 Mar 2 '12 at 14:27
add comment

2 Answers

up vote 0 down vote accepted

I would keep an associative array (Object) where you store which events are active. Whenever an event is triggered or finalized (which are the only two situations which produce the changes you want to track) you add/remove an element to the object and the print its contents with a loop like the following:

logMsg = ""
for(eventName in activeEvents) logMsg += " timestamp"+timestamp+": "+eventName;
share|improve this answer
I accept this answer since it is what I am doing, even thought my question really was if there was a smarter way of doing it, thanks for the feedback. –  user1245172 Mar 6 '12 at 20:08
add comment

I understand, that an event here is something with a starting time and a duration. And when an event starts or ends, you want to draw a list of the remaining events.

The data structure to use could be a set (unordered list). And whenever an event starts or ends, you add or remove the event to/from the set and take a listing of the remaining set as result.

Is that what you meant?


I saw the javascript tag just now.

var events = {
    getEvents: function() {
        var result = [];
        for (key in this) {
                    result.push(key); }
        return result;
// add events
var event_A = 'A', event_B = 'B';
events[event_A] = true; // or whatever
console.log(events.getEvents()); // ['A']
events[event_B] = true; 
console.log(events.getEvents()); // ['A', 'B']
// stop events
delete events[event_A]
console.log(events.getEvents()); // ['B']
share|improve this answer
My current implementation is something like this, my question is if there is a better way, that needs less regeneration when new events are added, and has higher performance. –  user1245172 Mar 2 '12 at 14:32
Did you profile, that this step is the performance bottleneck? –  jammon Mar 2 '12 at 14:41
add comment

Your Answer


