-1

I use JQuery to code in JavaScript. It's easy, less coding, more readable. When we compare JQuery to pure JavaScript, the JQuery is around 80% slower than pure JS.

So, JQuery (or other JS library) is a very good thing for the developer but at the end become a bad thing for the user : slower to load, slower to run.

JavaScript is running on the client side.

So, in my opinion, if I want to code easily with a frameworks it's my business.

It the developer's problem and the client don't have to see the difference.

I am wondering if is possible to code in JQuery, and compile it (locally) on a pure JavaScript.

I mean something like lesscss.


DOM access with Jquery

this JQuery code is faster to write but is 80% slower than pure JS : performance report

for example, the JQuery code :

   $('#mydiv').css('backgroundColor', 'red');
   $('#mydiv').hide();
   $('#mydiv').html('hello');

   $('#content .col').each(function () {
       $(this).html('ok')
   });

would be compiled to pure JS :

 document.getElementById('mydiv').style.backgroundColor = 'red';
 document.getElementById('mydiv').style.display = 'none';
 document.getElementById('mydiv').innerHTML = 'hello';

 var query = document.querySelectorAll('#content .col');
 for (var i = 0; i < query.length; i++) {
      query[i].innerHTML = 'ok';
 }

prototype.js class declaration

this prototype.js déclaration is faster to write but is 98% slower than pure JS : performance report

for example the prototype.js code :

var Animal = Class.create({
  initialize: function(name, sound) {
    this.name  = name;
    this.sound = sound;
  },

  speak: function() {
    alert(this.name + " says: " + this.sound + "!");
  }
});
var cat = new Animal('Kitty', 'Meow');
cat.speak();

would be compiled to pure JS :

function Animal(name, sound) {
  this.name = name;
  this.sound = sound;
}

Animal.prototype.speak = function() {
  result = (this.name + " says: " + this.sound + "!");
}

var cat = new Animal('Kitty', 'Meow');
cat.speak();

At the end, the question is about to have good tools to code quickly without impact on the user.

18
  • 3
    Do you have any real speed concerns on a real project with a jQuery based ria or is this only benchmark porn? Commented Mar 28, 2014 at 11:42
  • "is it possible to pre-compiling Jquery to native JavaScript ?" jQuery is not a language, it's a library of functions written in JavaScript. You may mean "native DOM" or "native browser." Commented Mar 28, 2014 at 11:42
  • 2
    The short answer is No Commented Mar 28, 2014 at 11:48
  • Are you using a common public CDN, like google, for the jquery script src URL? Commented Mar 28, 2014 at 11:50
  • 1
    "I want to use a library like JQuery to ease coding but it's look like a bit slower". Maybe it's a problem with your jQuery-fu. Any chance you could show us that way to slow code so we could suggest some improvements? Commented Mar 28, 2014 at 13:32

1 Answer 1

1

I've not had any personal experience with this, but Google Closure Compiler might be your best bet.

https://developers.google.com/closure/compiler/?csw=1

"It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left."

This should make your code more efficient, although I'm not sure how effective it would be at specifically optimizing jQuery. It is unlikely you'll actually get fully "native" JavaScript.

Sign up to request clarification or add additional context in comments.

6 Comments

@DirkLachowski In no way? OP is essentially asking for a way to inline and unroll and optimize the jQuery library code. This points to a program that might have the best shot at doing so.
@delnan Nope, OP is asking for an (automatic) way to replace jQuery functions by native DOM manipulation. I can't see how the closure compiler should do that in a browser independent way.
@DirkLachowski At the end of the day jQuery calls those native DOM functions, it's just hidden beyond several layers of function calls and data structures. And browser feature testing, yes, but that's a minor point and the equivalent hand written native-DOM-manipulating code would have to do that too. Eliminating those is the holy grail of compiler optimization. I actually doubt the Closure compiler will actually succeed at it, but it's not completely off.
@delnan I agree (btw. this woodworking vs. meat pie comparison was really funny, sad you removed it). But i think you got my point - in the OPs problem the closure compiler will not help (well, maybe on IE6 or some hardware from the dark ages but that's another story).
@Lacius in my mind, Google closure compiler is more like a minifier than compiler. It's minify the code but don't really improve the internal performance.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.