Posts

Showing posts from March, 2024

Nugget 5: JavaScript Function Factory and Closure

Image
 A function factory in JavaScript is a function that returns an object.  For this Nugget, I created a financial portfolio page to illustrate Function Factory and Closure in JavaScript. I had too much fun doing this. FinancialStock () is a function factory that creates an equity stock object.  Every time this function is invoked, a new object is created.  Note that all objects will have the same prototype; they all have the same properties and the same stockFluctuation() calculation function. When the page is loaded, a new factoryManager() object is created, and when the "Add" button is clicked then the addSymbol () function is invoked and the factoryManager creates a new equity symbol. The Closure When a Closure is created in JavaScript, a function can access values outside of its own curly braces, outside of its scope.  This happens every time a new equity symbol is created. Note that createSymbol() has access to the variable indexId which is used as the sym...

Nugget 4: StringBuilder Keeps Its Crown

Image
  String interpolation was introduced in C# version 6.  In this Nugget I wanted to benchmark string concatenation using the plus (+) operator, StringBuilder append, and string interpolation.  For this test, I used a variation of the class used by Tim Corey in his video " Intro to Benchmark.net - How To Benchmark C# Code ". Based on the benchmark result using BenchmarkDotNet sown below, it is clear to me that StringBuilder is still king! References: Nugget 3: Performance Testing with BenchmarkDotNet

Nugget 3: Performance Testing with BenchmarkDotNet

Image
Collections in our data model are primarily represented by arrays. This Nugget intends to explore the performance of adding new items to a child collection of a complex object. A simple project representing the NHL was created for this test. The solution explorer looks like this: The Array Extension extension method is used to hide how a new item is added to the array. Note that using this extension affected the performance of adding new Players to the PoolPlayer array collection. Is this significant? It is only telling me that it needs careful attention. For this reason, the method using the extension was used as a baseline. The other two methods were benchmarked against this method. Reference 1 in this Nugget explains how the run is done. BenchmarkDotNet even gives us an estimated running time. Conclusion From the run, it seems that using a List to add new items to the collection is more efficient. Although all methods execute similarly, based on the Ratio using the lis...

Nugget 2: Force a Computed KO observable to re-compute by using notifications

Image
What is interesting in this nugget is that there is communication between the above two models via a computed observable property in one of the models. Note that wristWatch has a reference to an observable property of the injected model, question . Line 5 prints true because the property is observable. When askTime in the curiousVisitor model is invoked all subscribers to question are notified. Because this observable is now a dependent observable in line 8, the observable is emitted here, in the body of the computed observable, and the property is recalculated.

Nugget 1: Use the Spread Operator to add a new property to an object

Image
When using the spread operator to clone an object in Javascript, primitive properties (numbers, boolean, string) are copied. However, objects are not copied and what the clone gets is a shallow copy. Beware.