SharedFileView is a Node.js add-on that loads a file into a shared memory segment, allowing multiple processes to access the same memory data quickly and efficiently, as a JavaScript array. The returned array is read-only.
SharedFileView is designed for speed and minimal memory usage. By using shared memory, SharedFileView eliminates the need to copy data between processes, resulting in blazing fast performance. Additionally, SharedFileView uses the minimum memory allocation possible to load the text file into memory, ensuring that your system resources are used efficiently.
It is necessary to have the Boost libraries version 1.81 or higher installed on the system. Failure to have the required libraries installed will result in installation failure.
To install the Boost libraries, please follow the instructions outlined in this guide. Ensure that the environment variables BOOST_ROOT
and BOOST_LIBRARYDIR
are correctly configured for the installation to proceed smoothly.
npm install shared-file-view
First create a SharedFileView for an existing file, using the SharedFileView.Create
static method. This method is asynchronous, so you can pass a callback function as the second argument to be called when the SharedFileView is actually created.
const { SharedFileView } = require("shared-file-view");
SharedFileView.Create("/path/to/file.txt", (err) => {
if (err) {
console.error(err);
} else {
console.log("SharedFileView created");
}
});
To retrieve an array of lines from a file, use the SharedFileView.ArrayFrom
constructor. This method returns a JavaScript array that you can use to access any line of the file.
const { SharedFileView } = require("shared-file-view");
const filePath = "/path/to/file.txt";
const sharedFileView = new SharedFileView.ArrayFrom(filePath);
console.log(sharedFileView[0]); // Prints the first line of the file
console.log(sharedFileView[1]); // Prints the second line of the file
To check if a SharedFileView for a file has already been created, use the SharedFileView.Exists
static method. This method returns a boolean indicating whether the SharedFileView exists.
const { SharedFileView } = require("shared-file-view");
const filePath = "/path/to/file.txt";
const exists = SharedFileView.Exists(filePath);
console.log(exists); // Prints true if a SharedFileView exists, false otherwise
To remove a SharedFileView from memory and free up system resources, use the SharedFileView.Remove
static method.
const { SharedFileView } = require("shared-file-view");
const filePath = "/path/to/file.txt";
SharedFileView.Remove(filePath, (err) => {
if (err) {
console.error(err);
} else {
console.log("SharedFileView removed");
}
});
SharedFileView is licensed under the MIT License. See the LICENSE
file for details.
I would like to thank the following individuals for their contributions to SharedFileView:
- Seth Heeren (@sehe), for providing the original approach that underlie SharedFileView.
- Allen Luce (@allenluce), for creating the
mmap-object
package that inspired SharedFileView.