Topaz
Multithreaded Javascript Engine for .NET
Install / Use
/learn @koculu/TopazREADME

Topaz
Topaz is a high-performance, multithreaded JavaScript engine for the .NET platform. It is designed to overcome the limitations of existing JavaScript engines by providing robust multithreading support and syntax-level options that simplify script language usage. With Topaz, you can seamlessly execute JavaScript scripts on the server-side with blazing speed and harness the full power of the .NET runtime.
Why another Javascript Engine?
While existing JavaScript engines adequately handle script execution in single-threaded desktop environments, they often fall short when it comes to server-side execution. Topaz addresses this limitation by allowing for concurrent multithreaded usage, enabling efficient execution of JavaScript code in server applications.
To gain a deeper understanding of the capabilities and versatility of Topaz, I encourage you to check out the TopazView project.
Syntax-Level Options
Topaz offers syntax-level options that streamline the scripting support for various products. These options provide flexibility in managing variables, such as controlling assignment scope and disabling null reference exceptions, without disrupting the script's flow. By incorporating these options, Topaz ensures a more intuitive and convenient scripting experience.
Lock-Free Scripting Engine
One of the key goals of Topaz is to provide a lock-free scripting engine. By minimizing locking mechanisms, Topaz optimizes performance and eliminates potential bottlenecks, resulting in faster and more efficient script execution. This design choice makes Topaz an ideal choice for high-performance server applications that require concurrent execution of JavaScript code.
How Does Topaz Work?
Unlike traditional JavaScript engines like V8, which convert JavaScript code directly into machine code, Topaz takes a different approach. It parses the abstract syntax tree of the JavaScript code and executes instructions within the .NET runtime environment. By leveraging the power of the .NET runtime, Topaz achieves a seamless integration between JavaScript and .NET, enabling JavaScript variables to be treated as native .NET types.
While Topaz may not support the entire spectrum of JavaScript runtime features, it serves as a lightweight script interpreter that harnesses the full capabilities of the .NET runtime. This approach ensures compatibility with existing .NET libraries and provides access to the vast ecosystem of tools and functionalities available in the .NET ecosystem.
Key Features
- Multithreaded JavaScript execution for enhanced performance in server-side applications.
- Syntax-level options that simplify script language usage, providing greater flexibility and control.
- Lock-free scripting engine for optimal performance in concurrent execution scenarios.
- Seamless integration with the .NET runtime, allowing JavaScript variables to be treated as native .NET types.
Getting Started
Getting started with Topaz is quick and easy. Simply install the Topaz NuGet package and start leveraging the power of the multithreaded JavaScript engine for your .NET projects.
Join our vibrant community of developers and contribute to the project's growth by reporting issues, suggesting improvements, or submitting pull requests.
License
Topaz is released under the MIT License. Feel free to use, modify, and distribute the project according to the terms of the license.
The performance of Topaz
Topaz is designed to deliver excellent performance in JavaScript execution, surpassing that of other engines such as ClearScript and Jint. While we have focused on optimizing Topaz for a wide range of scenarios, it's worth noting that performance can vary based on specific use cases.
Environment:
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
Intel Core i7-6850K CPU 3.60GHz (Skylake), 1 CPU, 12 logical and 6 physical cores
64 GB DDR4 Memory
.NET SDK=5.0.402
[Host] : .NET 5.0.11 (5.0.1121.47308), X64 RyuJIT
Job-YXUPLH : .NET 5.0.11 (5.0.1121.47308), X64 RyuJIT
Benchmark 1 (1M for loop)
Simple For:
f1 = (i) => i * i
for (var i = 0.0 ; i < 1000000; ++i) {
f1(i)
}
Host For (Single-Thread):
Host.For(0, 1000000 , (i) => i + i)
Parallel For:
Parallel.For(0, 1000000 , (i) => i + i)
Result:
| Method | Mean | Error | StdDev |
|-------------------- |-------------:|-----------:|-----------:|
| Topaz Parallel For | 128.361 ms | 1.5664 ms | 1.4652 ms |
| Topaz Host For | 225.279 ms | 1.2184 ms | 0.9513 ms |
| Topaz Simple For | 465.666 ms | 3.4883 ms | 3.2630 ms |
| V8Engine Parallel | NOT SUPPORTED |
| V8Engine Simple For | 6.006 ms | 0.1129 ms | 0.1108 ms |
| V8Engine Host For | 2,647.886 ms | 32.2750 ms | 30.1901 ms |
| Jint Parallel For | NOT SUPPORTED |
| Jint Host For | 339.026 ms | 3.6222 ms | 3.2110 ms |
| Jint Simple For | 627.941 ms | 11.4362 ms | 10.1379 ms |
Comments:
ClearScript V8 Engine is super optimized for isolated scripts that don't communicate with the host. However, it is extremely slow (1M Loop in 8 seconds!) if the script code interacts with host. Topaz is significantly faster than Jint in simple for loops. Moreover, Topaz can iterate loops in parallel using .NET Task Parallel Library (TPL).
Benchmark 2 (1M manipulations on arbitrary .NET objects)
Code:
for (var i = 0.0 ; i < 1000000; ++i) {
model.Value++;
}
Result:
| Method | Mean | Error | StdDev |
|------------ |------------:|---------:|--------:|
| RunTopaz | 548.9 ms | 10.42 ms | 9.23 ms |
| RunJint | 616.7 ms | 4.62 ms | 4.10 ms |
| RunV8Engine | 30,598.6 ms | NA | NA |
Comments:
V8 engine is extremely slow because the script code interacts with host object. Topaz is slightly faster than Jint with manipulations on arbitrary .NET object.
Benchmark 3 (1M manipulation on Dictionary<,>)
Code:
for (var i = 0.0 ; i < 1000000; ++i) {
model.Value++;
}
Result:
| Method | Mean | Error | StdDev |
|------------ |-----------:|---------:|---------:|
| RunTopaz | 620.6 ms | 11.61 ms | 10.86 ms |
| RunJint | 730.9 ms | 4.31 ms | 4.03 ms |
| RunV8Engine | Error | NA | NA |
Comments:
Topaz is significantly faster than Jint in this benchmark. The model object is a Dictionary<string, int>.
Benchmark 4 (1M arbitrary .NET object method call)
Code:
for (var i = 0.0 ; i < 1000000; ++i) {
model.Increment();
}
Result:
| Method | Mean | Error | StdDev |
|------------ |-----------:|---------:|---------:|
| RunTopaz | 401.4 ms | 2.35 ms | 1.83 ms |
| RunJint | 448.6 ms | 1.07 ms | 0.90 ms |
| RunV8Engine | 8180.00 ms | 4.691 ms | 4.388 ms |
Comments:
Topaz is slightly faster than Jint in this benchmark. The model object is a .NET class. This benchmark demonstrates .NET function calls from script.
Benchmark 5 (100K arbitrary .NET objects manipulation and addition into an array)
Code:
var list = []
for (const item of model.Profiles) {
let x = {}
x.Name = item.Name + 1
x.Address = item.Address + 1
x.Bio = item.Bio + 1
x.City = item.City + 1
x.Country = item.Country + 1
x.Email = item.Email + 1
x.Phone = item.Phone + 1
list.push(x)
}
model.List = list
Result:
| Method | Mean | Error | StdDev |
|------------ |-----------:|---------:|---------:|
| RunTopaz | 866.2 ms | 200.4 ms | 132.6 ms |
| RunJint | 2,708.2 ms | 692.0 ms | 457.7 ms |
| RunV8Engine | 33,3009 s | 0.173 s | 0.512 s |
Comments:
Topaz is significantly faster than Jint in this benchmark. The model object is a .NET class. This benchmark demonstrates 100K object iteration and creation with several properties. Please note that using Topaz JsObject or an object that implements IDictionary instead of an arbitrary class would avoid reflection and boost the performance of your script. This is not added as a separate benchmark as it would not be fair to compare that with other engines.
Benchmark 6 (1M Javascript parallel function call from Host)
Code:
function square(x) {
return x*x;
}
// calls script function from .NET
Parallel.For(0, LoopLength, (x) =>
{
var result = engine.Invoke("square", x); ;
});
Result:
| Method | Mean | Error | StdDev |
|------------ |------------:|-----------:|----------:|
| RunTopaz | 224.1 ms | 4.39 ms | 4.70 ms |
| RunJint | 653.1 ms | 4.64 ms | 4.12 ms |
| RunV8Engine | 13,630.36 ms | 14.993 ms | 14.025 ms |
Comments:
Topaz is significantly faster than Jint on this benchmark. Jint functions are not thread-safe even when they do not manipulate any shared object. Topaz functions can be safely called from multiple threads in parallel.
The Evolution of Topaz
The story of Topaz is one of continuous improvement and the pursuit of excellence. It all started when I developed a server-side HTML page rendering application using Razor. However, as my microservice architecture and business needs evolved, I realized that Razor wasn't as flexible as I had hoped.
In my quest for a more optimized solution, I turned to ClearScript. While ClearScript relied on Google's v8 C++ engine, it suffered from performance issues due to locks and marshaling, resulting in sluggish response times. Even on my desktop, I could barely serve 100 requests per second, and memory consumption soared to 800 MB for just a few page renderings.
Seek
Related Skills
node-connect
334.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
82.3kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
334.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
82.3kCommit, push, and open a PR
