Twitter relative time parsing

JavaScript performance comparison

Test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
  var tweets;
 
  $.getJSON('http://twitter.com/statuses/user_timeline/TheOnion.json?count=100&callback=?', function(data, textStatus, jqXHR) {
    tweets = data;
  });
 
 
  // https://gist.github.com/1123968
  function timetowords(timestamp) {
    var words = {
      "seconds": "less than a minute",
      "minute": "about a minute",
      "minutes": "%d minutes",
      "hour": "about an hour",
      "hours": "about %d hours",
      "day": "a day",
      "days": "%d days",
      "month": "about a month",
      "months": "%d months",
      "year": "about a year",
      "years": "%d years"
    },
        deltaMilliseconds = (new Date().getTime() - timestamp),
        seconds = deltaMilliseconds / 1000,
        minutes = seconds / 60,
        hours = minutes / 60,
        days = hours / 24,
        years = days / 365;
 
    return seconds < 45 && words.seconds.replace(/%d/i, Math.round(seconds)) || seconds < 90 && words.minute.replace(/%d/i, 1) || minutes < 45 && words.minutes.replace(/%d/i, Math.round(minutes)) || minutes < 90 && words.hour.replace(/%d/i, 1) || hours < 24 && words.hours.replace(/%d/i, Math.round(hours)) || hours < 48 && words.day.replace(/%d/i, 1) || days < 30 && words.days.replace(/%d/i, Math.floor(days)) || days < 60 && words.month.replace(/%d/i, 1) || days < 365 && words.months.replace(/%d/i, Math.floor(days / 30)) || years < 2 && words.year.replace(/%d/i, 1) || words.years.replace(/%d/i, Math.floor(years));
  }
 
 
 
  // Part of https://github.com/seaofclouds/tweet
  function relative_time(date) {
    var relative_to = new Date();
    var delta = parseInt((relative_to.getTime() - date) / 1000, 10);
    var r = '';
    if (delta < 60) {
      r = delta + ' seconds ago';
    } else if (delta < 120) {
      r = 'a minute ago';
    } else if (delta < (45 * 60)) {
      r = (parseInt(delta / 60, 10)).toString() + ' minutes ago';
    } else if (delta < (2 * 60 * 60)) {
      r = 'an hour ago';
    } else if (delta < (24 * 60 * 60)) {
      r = '' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
    } else if (delta < (48 * 60 * 60)) {
      r = 'a day ago';
    } else {
      r = (parseInt(delta / 86400, 10)).toString() + ' days ago';
    }
    return 'about ' + r;
  }
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
timetowords
$.each(tweets, function(i, tweet) {
  timetowords(Date.parse(tweet.created_at));
});
pending…
seaofclouds / tweet
$.each(tweets, function(i, tweet) {
  relative_time(Date.parse(tweet.created_at));
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment