A very unoptimized calculator written in Javascript, generated by yet another unoptimized program written in Java.
Be sure to watch out for all the "unoptimization" comments and easter eggs!
Based on these reddit posts from r/ProgrammerHumor
- Calculator Challenge Accepted
- I got bored and decided to do the same thing as that really long python calculator. Except in Java
Disclaimer: I am a competent programmer. All unoptimizations, commented on or not, were intentional :)
The calculator is created by building, at runtime, a huge Javascript function that calculates the sum, difference, product, or quotient of two Java Integers
.
This function is then run through Java's Nashorn Javascript engine and called.
This already provides 2 source of unoptimization: the building of the JS program and the emulation of it running.
Futhermore, the JS function is built using very ineffective (and horrible by pretty much every programming standard) means.
As seen in the original reddit posts, the function is a series of if-statements, each returning the result of that calculation.
This series of if-statements is built using Java's string concat
enation operaters rather than a StringBuilder
or StringBuffer
.
And as some programmers may know, this is one of the least efficent ways to concatecation Strings (an even worse way could be to manually handle the char arrays).
Since the program must accept any 2 integers for math, the program must construct
(Integer.MAX_VALUE
× 2 + 1)2 × 4 = 73786976260478468100
or 73 quintillion if-statements, and then concate and process them all together.
Benchmarking on my computer resulted in an approximate speed of 1000 if-statements per second. This would mean that it would take about 2⅓ billion years just to construct the entire program.
I think it is safe to say that this program is very inefficent.