TAG | Javascript
During the first round of development for JSOQ I needed a faster way to test than unit-tests alone could provide. What I needed was a dirt-simple page that I could run in a browser, enter some JavaScript code, click a button and see it execute.
I built the initial JSOQ Console (which is still included with the JSOQ source code) in about 20 minutes and it increased my productivity by 10x.
It was much faster to test JSOQ functions with the JSOQ Console than it had been with unit-tests. As a result I was able to prototype new functionality in about 1/2 the time.
I’ve finally moved v0.2 of the Jsoq Console to my blog here so you can use it to quickly test out your own JavaScript code!
How it works is pretty straight forward. If you’ve got a snippet of JavaScript, be it jQuery (for the moment jQuery is the only external library that is supported but that will change soon), JSOQ or any other javascript code, you can run it with the JSOQ Console.
Just navigate to http://tinyurl.com/jsoqconsole or http://codeimpossible.com/jsoq for those of you that like to type, paste your code into the textarea at the top of the page, press “Run” and your code will run!
So enjoy! If you get confused there are also samples that will show you how to use the more “advanced” features. Also, drop a comment or share the console with others if you thought it was useful!

Project Euler is an awesome website that I found out about a while back via this question on StackOverflow. Basically, it’s a website that poses a ton of programmer related challenges and it keeps track of the problems that you solve.
The problems start out relatively easy (Add all the natural numbers below one thousand that are multiples of 3 or 5.) and progress all the way to insane (Caradano Triplets and Covex Holes).
I’m on the easy side, I just started working through them tonight and thought it would be cool to post how I build the solution to each problem as I complete them.
Problem 2 is pretty easy:
“Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.”
So first things first, I’ll need to pick a language to use to solve this. I chose javascript simply because it was something I knew I could get running pretty quickly.
Next I’ll need some variables. (note: if you’re not familiar with Fibonacci numbers Wikipedia has an awesome article on them.)
var start = 1, end = 4000000, current = 1, previous = 1, sum = 0, temp = 0;
That should do. I’ve got my start variable (dropping the 0 in favor of more readable code), my end counter, and my “where-am-i” variables: current, previous, temp and my sum variable.
Obviously we’ll need some kind of loop. How about a while loop?
while( current <= end )
{
}
Cool. Now when the loop is running I'll need a way to keep track of the current value, the previous value and I need to add the current value to the sum. So....
while( current <= end )
{
temp = current;
current = current + previous;
sum += current;
previous = temp;
}
And there we hav... Oh Wait!
I overlooked a pretty important part of the problem description: "sum of all the even-valued terms"!!!
So given this new(ish) piece of info my solution end sup being:
var start = 1, end = 4000000, current = 1, previous = 1, sum = 0, temp = 0;
while( current <= end )
{
temp = current;
current = current + previous;
if( current%2 == 0 )
sum += current;
previous = temp;
}
alert(sum);
So, yeah I originally started writing this article back in september when I was about mid-way through JSOQ. Some things have changed since then and I’ve tried to keep the post up-to-date. Enjoy!
Also, another similar library has come out recently: JSINQ which is a really feature-rich LINQ to Object implementation in Javascript. Really bad-ass stuff.
The Elevator Pitch:
JSOQ is an open-source pet project of mine that lets you easily access the data that you need from large collections in JavaScript. It was developed over a two week period in September of 2008 and is currently at version 1.1.
Everyone probably just shrugged and said “so??” Well let me show you.
Lets assume that you have the following JSON string from another web application like, say twitter search results for example:
{"results":[
{
"text":"Just another day at the office",
"to_user_id":null,
"from_user":"codeimpossible",
"id":1194587890,
"from_user_id":1316793,
"iso_language_code":"en",
"profile_image_url":"www.path.to.image.us",
"created_at":"Tue, 10 Feb 2009 05:47:33 +0000"
},
{
"text":"@spolsky thats easy! http:\/\/tinyurl.com\/2z42bs",
"to_user_id":1357501,
"to_user":"spolsky",
"from_user":"codeimpossible",
"id":1194582705,
"from_user_id":1316793,
"iso_language_code":"en",
"profile_image_url":"www.path.to.image.us",
"created_at":"Tue, 10 Feb 2009 05:44:51 +0000"
}
]}
In the sample above we only have two results but JSOQ has been tested to work with collections of tens of thousands of items.
The JSON object we have here is perfectly fine if we watned to show a whole bunch of data to the end user, but what if I only wanted to show the search results after a certain date? Or what if I only wanted the text from each message that was typed to this “spolsky” character?
To accomplish this in “straight” javascript I’d be looking at writing a lot of loops and if/else code and then I would still have all these extra properties that I don’t even need.
JSOQ allows me to query a javascript object or collection of objects using a specified criteria and return only the properties/methods I want.
So lets go with the second option. Let’s get all the .text members from all the messages that were sent to the “spolsky” user.
var search_results = eval(our_json_result);
var result = null;
with(jsoq) {
result = From(search_results.results)
.query('text')
.where(function(i) {
return i.to_user === "spolsky";
});
}
And now our result object is filled with a bunch of other objects that would look something like:
{
text: "@spolsky thats easy! http:\/\/tinyurl.com\/2z42bs"
}
Pretty simple eh? For more information on JSOQ please check back here in the near future or contribute to the google code repository.
It’s called “Chrome”. It happens to be made by Google. It’s just. another. browser. Are we done? Can we move on as a society?
EDIT: ok, so Chrome is fast. Really fast. I loaded Oliver Hunt’s JavaScript Ray Tracer in FireFox 3.0.3 and it took 30.393 seconds to complete a render. Under Chrome the render took less than 6.5 seconds.
