My problem with JavaScript libraries

Published by patrick on 08 Apr 2009 01:18 -0700. 3 Comments. Tags:

I have a problem with JavaScript libraries. My problem is this: I feel that, in a way, they begin to replace actual JavaScript for new web developers. Let’s say I teach a person to use jQuery. I teach him to use the various functions, and how to use plugins. Then, I see him working on a project one day and see this code:

<script type="text/javascript">
$(function() {
$("#hide").hide();
$("#show").show();
}
</script>

That’s right, he’s importing 20kb of pure JavaScript awesomeness for… two lines of code that could have just as easily been done using raw JavaScript:

<script type="text/javascript">
document.getElementById('hide').style.display = 'none';
document.getElementById('show').style.display = 'block';
</script>

There’s a plugin for jQuery that allows for slideshows known as jQuery Cycle. It weighs in at a hefty 28kb when compressed. However, when I see it in the wild, often I see that they only use it for the base fade in/out feature.

There’s a version of jQuery Cycle aptly named jQuery Cycle Lite that provides only this function, leaving out the many cool but underused features of the full version. This one weighs in at 3kb compressed.

Recently, my friend, colleague, and fellow writer at our blog Lateral Code, Karthik Viswanathan, wrote a very simple jQuery-based slideshow. It only does a fade in/out effect, but it only takes 300kb uncompressed. Take a look:

<script type="text/javascript">
$('.ppt li:gt(0)').hide();
$('.ppt li:last').addClass('last');
var cur = $('.ppt li:first');

function animate() {
cur.fadeOut( 1000 );
if ( cur.attr('class') == 'last' )
cur = $('.ppt li:first');
else
cur = cur.next();
cur.fadeIn( 1000 );
}

$(function() {
setInterval( "animate()", 5000 );
} );
</script>

That is it. That’s literally everything he needs other than jQuery to get that working. Do web developers need to reconsider their approach to JavaScript? Or is it fine to just continue plodding along teaching our pupils that JavaScript libraries are the best, letting them import 20kb just to hide an element?

3 Responses to “My problem with JavaScript libraries”

  1. Akshay 2009-04-27 23:50:08

    Why is using libraries wrong?
    Forget scripting, even in c++, java or anything else, the whole point of libraries is to minimize code use/make it easier to read/write.
    Its easy to code a smtp client with winsock, but most people just link to a library.
    I look at it like an opensource language, where libraries are just additions

  2. Patrick Lin 2009-04-29 18:33:28

    I agree that libraries are great for minimizing code use or making something easier to write. My problem is when you import a whole library for something extremely simple, like the examples provided.

  3. beebe 2009-05-07 14:10:24

    I agree with you. Libraries are vastly overused for simple tasks. Developers should consider libraries like a carpenter might consider any of the tools is his bag: use them when appropriate, leave them in the bag when not needed.

Leave a Reply
Comment Guidelines ↓
  • Please provide your name, you may use an alias if you wish to remain anonymous.
  • Comments with obviously fake emails will be deleted.
  • If you do not have a website, please leave that field blank.
  • Please keep comments related to topic. No spam please!
  • Please use proper grammar and spelling or your post may be edited or deleted.
  • If possible, please comment in English.
  • Un-related and spam comments will be deleted.

You may fill in this CAPTCHA to by-pass comment moderation.

Top ↑