CoffeeScript is 4 times shorter than C

2013-08-30

Compare FreeBSD’s sed.c (implemented in C) with drj’s sed.js (implemented in CoffeeScript).

A size comparison

              lines bytes
C             2471  62k
CoffeeScript  533   14k

That right there is CoffeeScript's chief advantage over C. The CoffeeScript is 4 times smaller, meaning CoffeeScript programs are likely to be faster to write, easier to maintain, and contain fewer bugs.

Is it an apples to apples comparison? The FreeBSD sed implements a couple more options than my mostly POSIX compliant sed.js but they don't pad it out much. Neither implementation includes code to handle Regular Expressions. sed.c uses the Unix library and sed.coffee uses JavaScript's built-in RegExp class. So I think it's a pretty reasonable comparison.

What makes the C code bigger? There is a lot of memory management, and an awful lot of string copying and substring extraction. There is an implementation of a hash table (for labels).

Obviously we all knew that high level languages resulted in smaller programs, but it's rare to get the opportunity to do such a direct comparison.

About these ads

3 Responses to “CoffeeScript is 4 times shorter than C”

  1. Gareth Rees Says:

    But how short would it be in J?

  2. Nick Says:

    Well, it’s only one line in shell. Let’s write our next web templating engine in shell then! (done that: html escaping is already a nightmare in such an environment)

    What you should look at is code complexity for realistic applications written in JS, like user interfaces: a simple UI to interface with a database, or compute some values. C would drown itself in extra lines to keep it readable, and because of its lack of automatic memory management. C++ would already be a lot better.


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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: