Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-shared-cache

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-shared-cache - npm Package Compare versions

Comparing version 1.2.0 to 1.4.0

test/counter.js

2

package.json
{
"name": "node-shared-cache",
"version": "1.2.0",
"version": "1.4.0",
"description": "Interprocess shared memory cache for Node.JS",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -69,4 +69,11 @@ ## node-shared-cache

test.self === test; // true
// release memory region (don't call when cache is still used by some process, may cause memory leak)
// release memory region
cache.release("test");
// increase a key
cache.increase(obj, "foo");
cache.increase(obj, "foo", 3);
// dump current cache
var values = cache.dump(obj);
// dump current cache by key prefix
values = cache.dump(obj, "foo_");

@@ -107,2 +114,30 @@ ```

### exported methods
#### release
function release(name)
The shared memory named `name` will be released. Throws error if shared memory is not found. Note that this method simply calls `shm_unlink` and does not check whether the memory region is really initiated by this module.
Don't call this method when the cache is still used by some process, may cause memory leak
#### clear
function clear(instance)
Clears a cache
#### increase
function increase(instance, name, optional increase_by)
Increase a key in the cache by an integer (default to 1). If the key is absent, or not an integer, the key will be set to `increase_by`.
#### dump
function dump(instance, optional prefix)
Dump keys and values
## Performance

@@ -109,0 +144,0 @@

var binding = require('../index.js');
var _t, hrtime = process.hrtime;
function begin() {
_t = hrtime();
}
function end() {
_t = hrtime(_t);
return (_t[0] * 1e3 + _t[1] / 1e6).toFixed(2)
}
// test plain object
var plain = {};
console.time('plain obj');
for(var i = 0; i < 1000000; i++) {
begin();
for(var i = 0; i < 1e6; i++) {
plain['test' + (i & 127)] = i;
}
console.timeEnd('plain obj');
console.log('write plain obj 100w times: %sms', end());
// test shared cache
var obj = new binding.Cache("benchmark", 1048576);
console.time('shared cache');
for(var i = 0; i < 1000000; i++) {
begin();
for(var i = 0; i < 1e6; i++) {
obj['test' + (i & 127)] = i;
}
console.timeEnd('shared cache');
console.log('write shared cache 100w times: %sms', end());
// test read existing key
console.time('read plain obj');
for(var i = 0; i < 1000000; i++) {
begin();
for(var i = 0; i < 1e6; i++) {
plain['test' + (i & 127)];
}
console.timeEnd('read plain obj');
console.log('read plain obj 100w times: %sms', end());
console.time('read shared cache');
for(var i = 0; i < 1000000; i++) {
begin();
for(var i = 0; i < 1e6; i++) {
obj['test' + (i & 127)];
}
console.timeEnd('read shared cache');
console.log('read shared cache 100w times: %sms', end());
console.time('read plain obj with key absent');
for(var i = 0; i < 1000000; i++) {
begin();
for(var i = 0; i < 1e6; i++) {
plain['oops' + (i & 127)];
}
console.timeEnd('read plain obj with key absent');
console.log('read plain obj with key absent 100w times: %sms', end());
console.time('read shared cache with key absent');
for(var i = 0; i < 1000000; i++) {
begin();
for(var i = 0; i < 1e6; i++) {
obj['oops' + (i & 127)];
}
console.timeEnd('read shared cache with key absent');
console.log('read shared cache with key absent 100w times: %sms', end());
// test enumerating keys
console.time('enumerate plain obj');
for(var i = 0; i < 100000; i++) {
begin();
for(var i = 0; i < 1e5; i++) {
Object.keys(plain);
}
console.timeEnd('enumerate plain obj');
console.log('enumerate plain obj 10w times: %sms', end());
console.time('enumerate shared cache');
for(var i = 0; i < 100000; i++) {
begin();
for(var i = 0; i < 1e5; i++) {
Object.keys(obj);
}
console.timeEnd('enumerate shared cache');
console.log('enumerate shared cache 10w times: %sms', end());
// test object serialization
var input = {env: process.env, arr: [process.env, process.env]};
console.time('JSON.stringify');
for(var i = 0; i < 100000; i++) {
begin();
for(var i = 0; i < 1e5; i++) {
JSON.stringify(input);
}
console.timeEnd('JSON.stringify');
console.log('JSON.stringify 10w times: %sms', end());
console.time('binary serialization');
for(var i = 0; i < 100000; i++) {
begin();
for(var i = 0; i < 1e5; i++) {
obj.test = input;
}
console.timeEnd('binary serialization');
console.log('binary serialization 10w times: %sms', end());
// test object unserialization
input = JSON.stringify(input);
console.time('JSON.parse');
for(var i = 0; i < 100000; i++) {
begin();
for(var i = 0; i < 1e5; i++) {
JSON.parse(input);
}
console.timeEnd('JSON.parse');
console.log('JSON.parse 10w times: %sms', end());
console.time('binary unserialization');
for(var i = 0; i < 100000; i++) {
begin();
for(var i = 0; i < 1e5; i++) {
obj.test;
}
console.timeEnd('binary unserialization');
console.log('binary unserialization 10w times: %sms', end());

@@ -19,2 +19,4 @@ var assert = require('assert');

var obj = new binding.Cache("test", 512<<10, binding.SIZE_1K);
binding.clear(obj);
console.log('set obj.foo');

@@ -30,7 +32,19 @@ obj.foo = "bar";

var nested = [process.env, process.env, {}];
obj.nested = nested;
console.log(binding.dump(obj));
assert.deepEqual(binding.dump(obj), {foo: 'bar', env: 0, nested: nested})
nested[2].test = nested;
// increase block
obj.env = [process.env, process.env];
obj.nested = nested;
assert.deepEqual(Object.keys(obj).slice(-2), ['foo', 'env']);
assert.deepEqual(Object.keys(obj), ['foo', 'env', 'nested']);
var result = obj.nested;
assert.strictEqual(result, result[2].test);
assert.strictEqual(result[0], result[1]);
for(var k in obj) {

@@ -40,11 +54,7 @@ console.log(k, obj[k]);

var test = [process.env, process.env];
test[2] = {'test':test};
obj.env = test;
obj.foo2 = 1234;
assert.deepEqual(binding.dump(obj, 'foo'), {foo: 'bar', foo2: 1234});
assert.deepEqual(binding.dump(obj, 'e'), {env: 0});
test = obj.env;
assert.strictEqual(test, test[2].test);
assert.strictEqual(test[0], test[1]);
delete obj.foo;

@@ -54,3 +64,2 @@ assert.ifError('foo' in obj);

console.time('LRU cache replacement');

@@ -57,0 +66,0 @@ for(var i = 0; i < 1000000; i+=3) {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc