Javascript Performance Playground

Javascript is surprisingly fast in modern browsers, but as for every language you will need to optimize at some point. I found a very useful website called JSPerf which provides a test framework, allows you to create and edit existing test framework, provides online persistence allowing to get wider results than if you ran them by yourself.

A simple example. I was wondering what was the fastest way to convert from a float to an int in Javascript. I had several options, and the best way was not as suggested by some colleagues. And the way to make sure what is the fastest way is to benchmark, and not to guess. The results of the benchmark are here, you can test on your machine by running the test. Your results will be stored and shared with everyone, enhancing the global knowledge. The fastest I found is the following, if you find faster, please let me know.

myInt = myFloat | 0

A second example. What is the fastest typed array to write and read integers? The results are here and sadly there is no fastest answer for all browsers. So if you do not want to complicate your code with browser tests and everything that goes with it, the best solution is to use the following.

var myArray = new Int32Array( size );

A third interesting example about the fastest way to clear a buffer and copy it in a canvas every frame. The results are here. The fastest way is to create a working buffer outside your frame, create another one initialized with the values you want, and copy using the method set every frame.

At initialization time

var external_data_1024 = ctx.createImageData(1024, 1024);
var external_source_1024 = ctx.createImageData(1024, 1024);

During the frame processing

external_data_1024.data.set(external_source_1024.data);
ctx.putImageData(external_data_1024, 0, 0);

Finally a word of caution, make sure you benchmark the right things.

  1. The initial float to int did not include the two solutions I added to the benchmark |0 and >>0, so having something that look fast does not mean you have found the optimal solutions.
  2. The original array speed test included array creation for the write, so the test was measuring creation+write instead of just write, and the results were very different. So make sure to measure what really matters to you.
  3. The last example was a subtle example of the points #1 and #2 above. The initial “fastest” function included unnecessary code in clear_canvas(), adding a new test without that code proved to be faster. The loop test was looping with a double indirection “data.data[i] = 0;”, removing that indirection made it much faster and measured the real filling time instead of the sum of filling time+overhead with “buffer[i] = 0;”.

Testing the performance of Javascript

I had an interesting conversation last Sunday with a friend about the different technologies available on the web, and we talked about benchmarking them with simple things. So I decided to give Javascript a try, and here is what I came up with. You can get the source code here.


To give credit where it is due, I looked at the code of this sample to get a quick start and learn the basics. I will share my learnings in a future blog post.

Logitech Solar Keyboard K750

My original wired iMac keyboard delete key broke, so I considered replacing it with a wireless keyboard. Unfortunately the Apple keyboard is terrible for me without a proper numerical keypad, so I looked at alternatives and decided to get the Logitech Solar Keyboard K750 and I am really satisfied with it.

The look matches the iMac design, it is wireless with a proper numerical keypad, no need of batteries thanks to the solar panels, and it also has a unifying small usb connector so that you can connect all your logitech devices to the same usb instead of needing several ones.

If you use Bootcamp and Windows, you might have issues with F1, F2 keys not working properly. Just download and install Logitech’s Setpoint software, and everything will work as expected.

Trying Eclipse for C++

I have been doing a lot of Java and Flash programming recently and both development environment have been relatively enjoyable with the Eclipse framework, so I decided to give it a try for C++. Yes I know there are other alternatives for C++ like Netbeans, Codeblocks, Codelite, and even MonoDevelop now. But having to be familiar with a single GUI is actually quite enjoyable.

  1. First download and install MinGW it will be your compiler. Untick everything except C compiler and C++ compiler.
  2. Get the latest Eclipse CDT it is the eclipse environment customized for C++.
  3. Launch Eclipse, open the “File” menu, then “New”, then “C++ project”
  4. Expand the Executable box (click on the left of it, sometimes the triangle does not show up on Windows 7). Select “Hello World C++ Project”, put a name, select “MinGW GCC”, and click Finish.
  5. Go to the “Project” menu and select “Build Project”.
  6. You should now be able to run the project and see “Hello world” in the console.

This seems all good, but actually when you try the simplest things like printf, the environment will have many issues like text not showing up. If you google for it you will find pseudo solutions recommending setvbuf( stdout, NULL, _IONBF, 0 ); but this makes things worse and create a whole lot of issues. The best is really to emulate the VS behavior and to have a separate console if you need one.

  1. Create gdbinit.txt in C:\MinGW\bin and put “set new-console” without the quotes in it.
  2. Go to the “Run” menu, then “Debugger” tab, and put  “C:\MinGW\bin\gdbinit.txt” without the quotes in “GDB Command Line” and click “Apply”

Now your printf will output to the external console as they do in VS, and OutputDebugString will output text to the Eclipse console.

Overall although the printf issue is kind of bad, at the end Eclipse seem like a good environment. I like things like CTRL+SHIFT+N to automatically add a missing include file. Next time I need to write a small C++ tool, I will give it a more serious try.

Excellent free alternatives to Maple and Mathematica

I was in office the other day and we do not have a license for Maple nor Mathematica so I decided to try the free alternatives, and I found two excellent ones.

1. Euler Math Toolbox (71MB) is the best option. It provides excellent numerical algorithms like remez to find the minimax polynomial approximation of a function for example. And it is also capable of symbolic calculations like calculating integrals, derivatives, dealing with linear algebra problems. The plotting and multiline commands are not as slick as what you can do in Maple or Mathematica but it is still an excellent software. I found the minimax algorithm to be more robust and easier to use than the Mathematica one for example, and basically as good as the one implemented in Maple

Example to plot a function:

plot2d(“sin(x)”,-Pi,Pi,>insimg)

Example to define and multiply a matrix:

A:=[1,1;0,1]; A.A

Example to use Maxima for polynomial expansion:

&expand((x-1)*(x-2)*(x-3))

Example to get the degree 2 minimax polynomial approximating sin(x) between 0 and Pi:

x=equispace(0,Pi,50); {t,d}=remez(x,sin(x),2); P=polytrans(t,d)

2. WxMaxima (32MB) is part of the default Maxima installation and proved to me the closest and best alternative to Maple in terms of symbolic and matrix calculations. At the same time it lacks some important numerical algorithms, which is why I prefer Euler Math Toolbox overall. But since its multiline handling is closer to Maple I still end-up using it for anything requiring flow controls and loops.

Example to integrate/differentiate a function:

integrate(sin(x),x); diff(sin(x),x);

Example to plot two functions:

wxplot2d([sin(x),cos(x)], [x,-5,5]);

Example to define and multiply a matrix:

a:matrix([1,1],[0,1]); a.a;

Example of polynomial expansion:

expand((x-1)*(x-2)*(x-3));

Example of solving for polynomial roots:

solve(x^3-6*x^2+11*x-6,x);

Example of simple computation loop:

array(res,8)$
z:7$
for i:1 thru 8 do ( res[i]:mod(z^i,8) )$
listarray(res);

Simple way to backup folders on a local drive

I found a while ago that GoodSync was the best solution, however it is not really free with a very low limit of 100 files. I do not believe that software is worth $30, so I used AlternativeTo, and I was proven right with the quality of the free options available.

My requirements were the following:

  1. Free
  2. Ability to do offline backup on an external drive.
  3. Smart one way mirror with comparison of destination
  4. Configurable filters to not copy .svn folders for example
  5. Support of multiple sources and destinations

Seems like a pretty low bar, but I was surprised not to find a lot of genuine candidates. I am listing below only the ones worth talking about, most solutions were online only, and the ones I do not mention in general had terrible UI.

FileMenu Tools ended up being my pick. I had the software already installed, but never noticed that feature before. The interface is simple and functional, it provides an editable diff of the two folders before operating, and filtering was as easy as you would expect. You can save presets for different couples of folders. The only option it lacks is a saved list of folders for batches, but it was good enough for me.

PureSync emerged as one of the best at first, unfortunately it felt like written in VisualBasic with a terrible interface, poor stability, and ineffective auto-update.

WinDataReflector did not give enough insight into what was done in the backup and synchronization processes.

Toucan was also promising but lacked clarity about the basic options, and handling multiple folders as separate projects was cumbersome, and the filtering was again not well integrated in the workflow.

GFI Backup allows to backup and sync folder, however the backup would work for mirroring but it does not compare what was already done the previous time, and the sync option is not flexible enough to be configured one way.

Switching from Photoshop to GIMP. Yes really!

I own a copy of Photoshop CS3 purchased a while back. At the time Photoshop was the only real credible software, and GIMP was kind of a joke.

Things have changed, and GIMP since 2.8 has a robust single window mode, a pretty impressive set of filters, and even allows me to run my favorite Photoshop plugins inside Gimp.

So I decided to give the software a chance, evaluated it with the real kind of work I would do in Photoshop, as well as simpler fast random daily tasks. I have been impressed enough to adopt the software, and to put my copy of Photoshop on sale.

I recommend evaluating it, and making your own mind. If you do here is what I recommend trying.

  1. Download and install GIMP 2.8.1 from Partha’s websitehttp://www.partha.com/downloads/Gimp-2.8.1-32bit.exe
  2. Download and install the useful plugins from the same site: http://www.partha.com/downloads/Gimp-2.8.1-Plugins-32bit.exe
  3. Try HQ Rescale to get high quality upsized images
  4. Try LayerFX to emulate the photoshop layer effects
  5. Try Liquid Rescale to get smarter resizing respecting image details
  6. Try Upsize as another option to HQ Rescale
  7. Try Refocus to get the best out of blurry shots
  8. Try Save for Web to optimize images as you can in Photoshop
  9. Try all the others included in the default install:  Gmic, Refocus it, Resynthesizer, Saturation Equalizer, Wavelet Denoise
  10. Try some Photoshop plugins with PSPI. For example I recommend the awesome and free XiDenoiser and XiQuantizer

Converting videos not supported by Xbox 360

XMedia Recode is completely free and can convert ahead of time almost anything to a format playable on Xbox 360 without the need to transcode in real-time.

  1. Download and install recode
  2. Open the file you want to convert
  3. In the Format tab, select “Microsoft” in profiles, and “XBox 360 H.264” underneath
  4. In the Filters/Preview tab, make sure to select the right resolution for your TV, and the right audio track
  5. Click the “+ Add Job” button in the top tool bar
  6. Click the “Encode” button and you should end-up with a .mp4 playable by your Xbox 360

Playing your PC videos on Xbox 360

There are plenty of solutions that you could try, I am going to spare you some time. Use PS3 Media Server.

  1. Turn on your Xbox
  2. Install and PS3 Media Server
  3. Check the status tab until the Xbox appears
  4. Go to Navigation/Share settings and add folders by clicking the icon with a “+”
  5. Click restart server
  6. Go back to your Xbox go to the Video section, then “My video apps”, then “Video player”, then the “Select Source” menu should appear with the PS3 Media Server listed in. And you are done, just browse your folders and play the videos you like.