notes on javascript, linux, and more

3.30.2009

Cron for Javascript

This class provides some simple scheduling with callbacks.
/*  Copyright (C) 2009 Elijah Rutschman

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details, available at
<http://www.gnu.org/licenses/>.
/*

/*
a typical cron entry has either wildcards (*) or an integer:

.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12)
| | | | .---- day of week (0 - 6) (Sunday=0)
| | | | |
* * * * *

*/

var Cron = {
"jobs" : [],
"process" : function() {
var now = new Date();
for (var i=0; i<Cron.jobs.length; i++) {
if ( Cron.jobs[i].minute == "*" || parseInt(Cron.jobs[i].minute) == now.getMinutes() )
if ( Cron.jobs[i].hour == "*" || parseInt(Cron.jobs[i].hour) == now.getHours() )
if ( Cron.jobs[i].date == "*" || parseInt(Cron.jobs[i].date) == now.getDate() )
if ( Cron.jobs[i].month == "*" || (parseInt(Cron.jobs[i].month) - 1) == now.getMonth() )
if ( Cron.jobs[i].day == "*" || parseInt(Cron.jobs[i].day) == now.getDay() )
Cron.jobs[i].run();
}
now = null;
},
"id" : 0,
"start" : function() {
Cron.stop();
Cron.id = setInterval("Cron.process()",60000);
},
"stop" : function() {
clearInterval(Cron.id);
},
"Job" : function(cronstring, fun) {
var _Job = this;
var items = cronstring.match(/^([0-9]+|\*{1})[ \n\t\b]+([0-9]+|\*{1})[ \n\t\b]+([0-9]+|\*{1})[ \n\t\b]+([0-9]+|\*{1})[ \n\t\b]+([0-9]+|\*{1})[ \n\t\b]*$/);
_Job.minute = items[1];
_Job.hour = items[2];
_Job.date = items[3];
_Job.month = items[4];
_Job.day = items[5];
_Job.run = fun;
Cron.jobs.push(_Job);
_Job = null;
items = null;
}
}

// queue up some jobs to run
var j1 = new Cron.Job("* * * * *", function(){alert('cron job 1 just ran')})
var j2 = new Cron.Job("5 * * * *", function(){alert('cron job 2 just ran')})
var j3 = new Cron.Job("15 * * * *", function(){alert('cron job 3 just ran')})
var j4 = new Cron.Job("30 * * * *", function(){alert('cron job 4 just ran')})
Cron.start();

// Cron already running, but we can add more jobs, no problem
var j5 = new Cron.Job("0 * * * *", function(){alert('cron job 5 just ran')})

3.27.2009

Simple Javascript Benchmarking Class

var Benchmark = function( testee, iterations ) {
var _Benchmark = this;
_Benchmark.testee = testee;
_Benchmark.iterations = iterations || 1;
_Benchmark.time = -1;
_Benchmark.runtime = -1;

_Benchmark.run = function() {
var time = 0;
for (var i=0; i<_Benchmark.iterations; i++) {
var startTime = new Date();
_Benchmark.testee();
var endTime = new Date();
time += endTime - startTime;
}
_Benchmark.runtime = time;
_Benchmark.time = time / iterations;
};
}


How to use it:
function nothing(x) {
// does a bunch of nothing
if (x > 0)
var y = (Math.sqrt(x) * Math.sqrt(nothing(x - 1)));
}

var myBenchmark = new Benchmark(function(){nothing(10)}, 300);
myBenchmark.run();
alert("Average run time to do nothing: " + myBenchmark.time );

3.10.2009

Dvorak

I am typing using a Dvorak layout. It is difficult. My typing speed has been reduced drastically. So why Dvorak? It is supposed to eventually get better. Unfortunately, this message has thus far taken 5 minutes to type.

About Me

My photo
chicago, il, United States
I'm a software engineer by profession.

Labels