Hugoware

The product of a web developer with a little too much caffeine

jLinq Reloaded

with 5 comments

[jLinq Beta is now online – go check it out!]

My first ‘open source’ project I ever released was a Javascript library that allowed you perform LINQ style queries with arrays of objects. The library supported operators, extension methods and even memorized commands to reduce typing.

Overall, it did a good job of making it easier to select information that normally would need to be handled with a for loop. Unfortunately, it turns out jLinq is rather slow. I recently blogged about a performance evaluation that came back with less than flattering results.

However, this is exactly where ‘open source’ helps make better software. If I hadn’t released my code then I might never know how it could be improved. This is the sort of thing that inspires software developers to improve their code — and that is exactly what is did for me.

So for the past few days I’ve been completely rewriting jLinq from the ground up and the gains have been incredible to say the least.

  1. About 60% smaller (around 8kb compressed)
  2. 99% faster queries
  3. Easier to extend library
  4. Minty fresh scent (your results may vary)

If you’re looking for a download link then you’re going to have to contact me instead. The code should be released soon but for now I’m still writing tests and preparing to rerelease the project with a brand new site (and probably a new name).

[http://github.com/hugoware/jlinq-beta/]

There has been a lot of interest already in seeing the new code so I’ve started a new repository on github.com. This is beta code so I suspect I’ll have errors in it. Please feel free to contact me to log an issue on the site.

If you’re interested in how jLinq improved so much then read on…

jLinq… But On Caffeine

My first theory about why jLinq was so slow had to do with the use of evals in my code.

At the time, I didn’t understand you could pass functions as arguments, I certainly had no idea what enclosures were, so jLinq would build an entire query in a string and then eval it into a method that could be invoked against each record.

I also used eval to get values from records instead of just referring to them by their names as an index. I don’t know why eval is so much slower, but the difference is undeniable.

//using index names
var fast = function(obj, path) {
    path = (path+"").split(/\./g);
    var index = 0;
    while(obj != null && index < path.length) {
        obj = obj[path[index++]];
    }
    return obj;
};

//being lazy with an eval
var slow = function(obj, path) {
    return eval("obj."+path);
};

//looping 100,000 times
loop(100000, function() { fast(data, "name.first"); }); //128ms
loop(100000, function() { slow(data, "name.first"); }); //6.8 minutes

So what kind of improvement do these two changes result in? Big ones.

Single Argument Query (850 Records)
Previously: ~430ms
Now: ~10ms

Multi-Argument Query with Strings (850 Records)
Previously: ~2730ms
Now: ~35ms

Simple Join (850 Records)
Previously: ~6200ms
Now: ~135ms

The list of improvements goes on but I’ll talk about them more in later blog posts.

So What Is Next?

It’s screaming fast and much smaller but unfortunately it is too new to even suggest that it is put into production. I’ll definitely feel more confident in the code after I’ve finished the tests.

But I also plan to rebrand the library this time. Why?

As it turns out, the name LINQ has caused some confusion with non-.NET developers. I’ve heard that some developers see the name and say ‘Well, I don’t use .NET so I don’t need this.’, which is clearly not the response I’d like to hear.

jLinq isn’t only for web pages. In fact, the neat thing about jLinq is that it can be plugged into anything running Javascript and it works great. I’d like to see jLinq be exposed to more developers.

So, with a new name, new site and all brand new code I think there is a chance to redefine what jLinq can do for the community.

The code is mostly finished so now it’s time for the hard part… coming up with a good name…

Advertisements

Written by hugoware

August 8, 2010 at 8:53 pm

5 Responses

Subscribe to comments with RSS.

  1. One theory for the eval() approach being slower is because in most modern browsers, the JavaScript engine can JIT compile the code, but it cannot precompile the eval’s . I think I read about this somewhere but cannot find the exact link at the moment.

    In the meantime, take a look at this: http://code.google.com/p/v8/issues/detail?id=71

    Bryan Migliorisi

    August 8, 2010 at 9:52 pm

  2. Brian, thanks for the interesting article. I’m a little confused by the ‘resolution’ dismissing the slowness as ‘by design/working as intended’, but I don’t really have the background to question it.

    In any case, it still amazes me how just a couple tweaks can completely transform the speed of code execution.

    hugoware

    August 9, 2010 at 8:35 am

  3. You know, the name is important because I blindly ignored jQuery for a long time (really up to about the 1.3 release) simply because what I had seen really just dealt with traversing the DOM – and I really was comfortable with Prototype or just plain document.getElementById()… luckily for me I had the opposite reaction with jLinq – I immediately recognized what it was and how it could benefit me. In fact I found it because I was searching for JavaScript and LINQ.

    So whatever you do, I would hope that the rebranding helps not deter people away, but at the same time isn’t so generic and obtuse that I (or the hypothetical web developer searching) would never happen to find. If I think of a great name I will forward it along.

    In a (almost) completely unrelated topic: Have you tried using jLinq inside of node.js?

    goyuix

    August 10, 2010 at 2:39 pm

    • Recently Javier Lozano had asked me about jLinq working with node.js (not sure if he ever tested it). As far as I can tell it should since it is pure Javascript (nothing DOM related)

      As far as the name of jLinq, I’ve gotten a lot of feedback against rebranding. A lot of people feel the name conveys the purpose well. So I’m settling on just refreshing the logo 🙂

      hugoware

      August 10, 2010 at 3:31 pm

      • Glad to hear that I am not the only person who scratched his head when you mentioned dropping LINQ from the name. The term is easy to search for and conveys the intent rather well.

        However, I do think the name could be optimized to convey “JQuery” linkage by switching to something like “jQLinq”

        This makes the brand stand out from other “jLinq” projects.

        Daniel

        August 11, 2010 at 3:00 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: