Saturday, November 12, 2016

Prototype Pattern vs. Module Pattern Memory Benchmark

I watched Jonathan Mills Pluralsight course Practical Design Patterns in JavaScript, which is about a few of the Gang of Four design patterns. His demo on the memory usage of the Flyweight Pattern inspired me to benchmark the memory usage of the Prototype Pattern in contrast to the Module Pattern.

One of the pro’s of the Prototype Pattern is to reduce memory because functions are on the object’s prototype. When new objects are created then memory is conserved because the new object’s functions all point to the parent object. In contrast, the Module Pattern creates new objects and functions. There are other pro’s and con’s to using each pattern, but I would like to only focus on the memory usage of each pattern.

Taking Jonathan's snippet of code that measures memory, I repurposed it to measure the Prototype and Module Pattern. I have included the code that I used to measure memory as well as a table of the results of a few test runs.

I hope this benchmark can help you decide when to use each pattern.

Table of Results:
Objects Created
Prototype Pattern
Memory (MB)
Module Pattern
Memory (MB)
1
0.001832
0.002624
5
0.003224
0.004232
10
0.003552
0.005552
100
0.00932  
0.03148
1000
0.07924
0.294816
10000
0.298288
2.378696


No comments:

Post a Comment