paint-brush
Google’s Closure Compiler can slow code tooby@diegorbaquero
399 reads
399 reads

Google’s Closure Compiler can slow code too

by Diego Rodríguez BaqueroFebruary 4th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Benchmarking is important, I feel there’s not enough

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Google’s Closure Compiler can slow code too
Diego Rodríguez Baquero HackerNoon profile picture

Benchmarking is important, I feel there’s not enough

Closure Compiler Service is the easiest way to get started with the Closure Compiler. I honestly didn’t want to install it locally if they offered a free, quick and online service. I chose the Simple Optimization, choosing the Advanced mode would remove the Fibonacci function, after all, nothing is being done with the result.

Let’s benchmark it…

Original Code (standard style, ES6 const/let)

const time = process.hrtime()




function fib (n) {if (n < 2) return nreturn fib(n - 2) + fib(n - 1)}





for (let i = 0; i < 10; i++) { // 10 iterationsfor (let n = 0; n < 37; n++) { // fib(0) ... fib(40)fib(n)}}




let diff = process.hrtime(time)console.log(`Benchmark took ${diff[0]}s, ${diff[1]}ns`)diff = (diff[0] * 1e9 + diff[1]) / 10diff = [Math.floor(diff / 1e9), Math.floor(diff % 1e9)]

console.log(`Benchmark took ${diff[0]}s, ${diff[1]}ns per iteration`)

Results:



➜ node original.10.jsBenchmark took 6s, 140681979nsBenchmark took 0s, 614068197ns per iteration

Closure Compiler version:














var time = process.hrtime();function fib(a) {return 2 > a ? a : fib(a - 2) + fib(a - 1);}for (var i = 0;10 > i;i++) {for (var n = 0;37 > n;n++) {fib(n);}}var diff = process.hrtime(time);console.log("Benchmark took " + diff[0] + "s, " + diff[1] + "ns");diff = (1e9 * diff[0] + diff[1]) / 10;diff = [Math.floor(diff / 1e9), Math.floor(diff % 1e9)];console.log("Benchmark took " + diff[0] + "s, " + diff[1] + "ns per iteration");

Results:



➜ node compiled.10.jsBenchmark took 6s, 334487173nsBenchmark took 0s, 633448717ns per iteration

I ran the test many times. Compiler version is slower. Maybe not by a lot (~3%), but slower.

If we were to actually run fibonacci for real stuff, we would use a cache with preinitialized hard-coded values:


// First 79 values for fibonacci's functionconst cache = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393,196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465,14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296,433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976,7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272,139583862445, 225851433717, 365435296162, 591286729879, 956722026041,1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723,17167680177565, 27777890035288, 44945570212853, 72723460248141,117669030460994, 190392490709135, 308061521170129, 498454011879264,806515533049393, 1304969544928657, 2111485077978050, 3416454622906707,5527939700884757, 8944394323791464]



function fib (n) {return cache[n] !== undefined ? cache[n] : fib(n-2) + fib (n-1)}

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising &sponsorship opportunities.

To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!