To begin using Hamsters.js you must first initialize the library by calling hamsters.init
this function takes a single argument, an object called startOptions. Using the startup options detailed below are optional however they will give you more fine tuned control of how the library operates to get the best performance and usability for your specific needs.
Hamsters.js was built from the ground up to work in any environment that runs JavaScript (ECMA-262 6th Edition or newer), when calling hamsters.init
and not passing the startOptions object the library will default to the recommended settings depending on the environment the library is running in. Hamsters.js features graceful performance and feature degradation depending on environmental support, the Hamsters.js library will always use the most performant and optimal features available within the environment its running in.
hamsters.init();
To make use of the optional startOptions object you should read about the different startOptions the Hamsters.js library supports below.
var startOptions = {
maxThreads: Integer,
legacy: Boolean,
persistence: Boolean,
transferable: Boolean,
debug: Boolean,
Worker: Object,
parentPort: Object
};
maxThreads This optional argument will tell the library how many threads it can use, this is a global maximum and is independent of the number of threads defined per function, and independent of the number of threads available within an environment. If you do not pass this optional startup option the library will attempt to detect the number of threads available within the environment Hamsters.js is running in, if the library is unable to detect this the library defaults to a value of 4
.
persistence This optional argument instructs the library to use persistence, persistence mode forces threads to be initialized at start up and persist so threads are reused. If persistence is not enabled the library will instead spawn threads on the fly when they're used and subsequently destroy threads when they are no longer needed. This has a default value of true
.
legacy This optional argument is useful for debugging purposes and instructs the library to run in legacy mode, if legacy mode is enabled the library will not spread computations across multiple threads but will simulate threading and follow the exact same process as is normally used making it easier to find and fix errors. This should have a value of false
during normal use.
transferable This optional argument is useful for debugging purposes and instructs the library to make use of transferable object support, this feature improves the overall performance of the library, by default Hamsters.js will automatically detect environment support for this feature and if present default to a value of true
.
debug This optional argument is useful for debugging and performance optimization purposes and changes the libraries logging level to give more information in the console, This has a default value of false
.
Worker This optional argument is for use with environments that do not include a native web worker implementation out of the box like Node.js and defaults to null
. Hamsters.js will automatically detect the ideal worker implementation and features to use depending on the environment it's running in, for environments that require a third party worker implementation you can pass the WebWorker object and the library will use make use of that WebWorker implementation.
parentPort This optional argument is for use with Node.js and defaults to null
.