Twitter relative time parsing

JavaScript performance comparison

Revision 2 of this 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(a){var b=new Date,c=parseInt((b.getTime()-a)/1e3,10),d="";return c<60?d=c+" s":c<120?d="1m":c<2700?d=parseInt(c/60,10).toString()+"m":c<7200?d="1h":c<86400?d=""+parseInt(c/3600,10).toString()+"h":c<172800?d="1d":d=parseInt(c/86400,10).toString()+"d",d}

</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