Gulp vs. Grunt – The Battle Of Build Scripts
I’ve already written about how to get started with Gulp as well as how to get started with Grunt. They both automate our tasks, they both use Node, and they both require you to create tasks and install plugins of some sort. But do you wonder about the difference between the two, or even which is better?
In this article, I’ll focus mainly on the differences between these two projects, which may factor into helping you decide which of the two you may deem better for yourself. I will be using some code that may be unfamiliar. If it is, I suggest reading through the two previously published articles before you get started.
Related:
Speed
The main difference between Gulp and Grunt lies in how they deal with their automation tasks on the inside. Gulp uses Node streams, while Grunt uses temp files. Let’s put that into plain English, shall we?
Assume you would like to write SASS code for your project. You would want to compile your SASS code and then perhaps minify it.
Grunt handles this using intermediary files which are disk I/O operations. Your SASS file is compiled and then written to a temporary file. The temporary file is used by the autoprefixer and then the final product is written to the destination file.
Gulp takes care of all this in-memory. Your source SASS file is compiled, the result is passed to the autoprefixer without being written to a file, and the destination file is then written out.
Compared to in-memory operations, disk writes are slow which means that Gulp has a big speed advantage (for now). A speed comparison was made by tech.tmw, which shows that most tasks are at least twice as fast on Gulp. While this wasn’t a hugely scientific study, the tendency is there, and I’ve seen the same with my own projects. But how big of an impact is the difference in speed?
Difference In Seconds
For most projects, this won’t matter. Most projects are small. When you’re creating a WordPress theme or something similar, the number of files you need to work with is well within a reasonable limit. It really doesn’t matter if your stylesheets are compiled in 400ms or 800ms.
Furthermore, most projects can be structured in such a way that some of the most intensive issues can be sidestepped. If you have 50 SASS files, you can just as quickly concatenate them while in development, there won’t be a need to autoprefix or minify them. You will not need to optimize images each time you save a project and so on.
Even when you really need the big guns because you’re pushing your work onto a staging server or when you’re updating a repository, does a built time of 5 seconds or 9 seconds make much of a difference?
To top it all off, Grunt will add support for piping in the upcoming 0.5 release which will speed things up considerably, making this a moot point.
The Community
Grunt has been around a lot longer than Gulp, so it has a significant user base. Grunt currently receives about 37,000 downloads a day on average, Gulp gets a bit more than half that, near the 23,000 mark. That being said, Gulp has only been around for a year and a half, making that number respectable, to say the least.
Grunt currently has over 4000 plugins while Gulp has more than 1200 plugins. According to Google trends more people search for Grunt related things, there are more forums that deal with it and generally more community support.
Of course, Gulp is up and coming, which means this is likely to even out in the long run. However, this is a barrier for some developers, especially those working on Grunt-based projects.
I would like to point out that the communities for both are extremely nice. As far as I can tell, the relationship between the leaders in each community is amazing and should serve as an example to all. The creator of Gulp actually helped the writer of the speed test comparison improve the timing accuracies, which led to a decrease in time differences. That’s what I call a gentleman!
Code vs. Configuration
Apparently, this is the tipping point for many, but to be honest, I can’t see the issue here personally.
The argument goes like this: Gulp is a good example that code over configuration can be a good thing when configuration gets a bit confusing. Other people say that while this is true and Gulp is easier to read, it is more difficult to write because piping can be a bit confusing.
Before I weigh in, here’s the same example first in Grunt, then in Gulp:
grunt.initConfig({ sass: { dist: { files: [{ src: 'dev/*.scss', dest: '.tmp/styles', expand: true, ext: '.css' }] } }, autoprefixer: { dist: { files: [{ expand: true, cwd: '.tmp/styles', src: '{,*/}*.css', dest: 'css/styles' }] } }, watch: { styles: { files: ['dev/*.scss'], tasks: ['sass:dist', 'autoprefixer:dist'] } } }); grunt.registerTask('default', ['styles', 'watch']);
gulp.task('sass', function () { gulp.src('dev/*.scss') .pipe(sass()) .pipe(autoprefixer()) .pipe(gulp.dest('css/styles')); }); gulp.task('default', function() { gulp.run('sass'); gulp.watch('dev/*.scss', function() { gulp.run('sass'); }); });
My opinion is that it really doesn’t matter. Sure, if you’re used to the first way, you may need to spend some time figuring out the second, but this is true vice versa as well. So for me, the “it’s confusing” argument is completely invalid. Any new method you learn is confusing at first, but if you take the time to understand the logic of each, it all evens out.
That said, I personally prefer Gulp’s API because it is cleaner, and it reflects the way I think more closely than Grunt. This is of course, completely subjective and is not an issue with Grunt at all, it’s just my personal preference.
How to choose?
I don’t think there’s any question about the fact that both Grunt and Gulp are great tools and have helped people save countless hours of time over the years. Grunt is a bit slower for now but has a much bigger community. Gulp is faster and has a cleaner API but lacks the user base.
I think that the decision will ultimately come down to continuity, available plugins and preference.
(1) If you’ve been using Grunt/Gulp for a while now and you’re happy with it, there’s no reason to switch.
(2) If your project requires plugins which are not provided by Gulp, and you’re not prepared to write one yourself, you will need to go with Grunt.
(3) If the above two considerations do not apply to you, it will come down to preference. I suggest trying out both and seeing which one sticks with you.
As I said, I chose to use Gulp because I like its cleaner API better, but I am perfectly comfortable using Grunt if a project calls for it. What you should not do is read that Mr. Know-it-all said that Gulp is better and accept it. While there are differences, there is no clear winner, and both projects can and will coexist. Try them out and make up your own mind.
Note: you might also want to consider opinions from users like Keith Cirkel (A Javascript consultant), who advises you to use neither. In his interesting Why we should stop using Grunt & Gulp article, he suggests using npm.
Further reading
There are many other excellent articles about this topic. I would heartily recommend any of the following for further reading; it never hurts to read what others have to say!
- Grunt vs Gulp – Beyond the Numbers (special thanks for the clear examples)
- Gulp, Grunt, Whatever
- Choose: Grunt, Gulp or npm?
- Speedtesting Gulp and Grunt
- Why we should stop using Grunt & Gulp
- Build Wars (use arrows to navigate)
- No need to Grunt, take a Gulp of Fresh Air