In my XPages applications I frequently use SSJS objects (com.ibm.jscript.std.ObjectObject) to cache data. To check if the performance improves if I use java.util.HashMaps instead, I benchmarked the execution times of the following code snippets (SSJS):
All three code snippets do the same: they create and fill either a SSJS Object or a HashMap with different types of data / objects. For each of the snippets I measured the average execution times over 1000 runs, with n (=maximum loop index in the snippet) being 1000000 (1 million). The benchmarks were performed on a Domino 9.0.1 server using java.lang.System.nanoTime .
The ratios of the execution times are as follows:
- 154% for T[HashMap] / T[SSJS Object]
- 266% for T[HashMap with put method] / T[SSJS Object]
- 172% for T[HashMap with put method] / T[HashMap]
In other words:
- filling a HashMap took ~54% longer than filling a SSJS object
- filling a HashMap using the put method took ~166% longer than filling a SSJS object
- filling a HashMap using the put method took ~72% longer than filling a HashMap using the SSJS "." notation
My questions are as follows:
- I frequently see SSJS code where HashMaps are used to store data. Why not use the standard SSJS objects if they provide better performance?
- Why is it so much more inefficient to use the put method instead of the SSJS "." notation to set the value of a HashMap?