I'm working on a bookmarklet that will be cached for 24 hours only, after that time it will be reloaded from the original source. Is there something I could improve?

javascript:(function () {var currentTime=new Date();var month=currentTime.getMonth()+1;var day=currentTime.getDate();var year=currentTime.getFullYear();document.getElementsByTagName('head')[0].appendChild(document.createElement('script')).src = 'http://bookmarklet.example.com/js/bookmarklet.js?v='+month+'-'+day+'-'+year}());

2 Answers 2


Use timestamps.

It's faster since you don't need to get the date, month and year. In the following script, the only function called for the date is getTime(). Additionally, if you use the newer Date.now(), you skip creating a date object.

In the example, we get the current timestamp and modulo it by a day. The remainder is the milliseconds that have passed since midnight. We then subtract it from the current timestamp so that the timestamp for the url is always based on the day. The only time it changes is when it's the next day.

Write to read

Sure, you're building a bookmarklet. But that does not mean making it unreadable. Write to read. Let the minifier do the shrinking for you. Besides, the bookmarklets themselves are copy-pasted, and not loaded. There's no overhead in size.

So in the following script, we pluck out the url so it's easily editable. We also make some parts verbose for easy reading.

The script

var url = 'http://bookmarklet.example.com/js/bookmarklet.js?v='

var currentTimestamp = new Date().getTime(); // or Date.now()
var currentDayTimestamp = currentTimestamp - (currentTimestamp % 86400000);

var script = document.createElement('script');

script.src = url + currentDayTimestamp;

Perf tests

Well, if you are bent on performance, then here's some perf tests.


As Joseph The Dreamer mentioned, the version you work on should not be the one liner, work on a properly formatted version and then let the minifier do it's thing. Since you are going for low character count, I would definitely suggest to use a single var statement. I also tend to end my bookmarklets with void(0);.

javascript:(function (){
  var now=new Date(),
   document.getElementsByTagName('head')[0].appendChild( script ).src = url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.