Socket
Socket
Sign inDemoInstall

sse4_crc32

Package Overview
Dependencies
Maintainers
4
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sse4_crc32 - npm Package Compare versions

Comparing version 2.1.2 to 3.0.0

79

benchmark/1.single_1kb_length_buffer.benchmark.js

@@ -1,36 +0,67 @@

var SSE4CRC32 = require("../sse4_crc32"),
js_crc32 = require("crc32"),
crc, start_time, end_time;
/**
* @file Benchmarks using a single 1kB buffer
*
* @author Anand Suresh <anand.suresh@gmail.com>
* @copyright Copyright (C) 2007-2015 Voxer LLC. All rights reserved.
* @license MIT
*/
var Sse4Crc32 = require('../sse4_crc32');
var jsCrc32 = require('crc32');
var crc, startTime, endTime, i;
var TEST_BUFFER = new Buffer("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi mollis cursus metus vel tristique. Proin congue massa massa, a malesuada dolor ullamcorper a. Nulla eget leo vel orci venenatis placerat. Donec semper condimentum justo, vel sollicitudin dolor consequat id. Nunc sed aliquet felis, eget congue nisi. Mauris eu justo suscipit, elementum turpis ut, molestie tellus. Mauris ornare rutrum fringilla. Nulla dignissim luctus pretium. Nullam nec eros hendrerit sapien pellentesque sollicitudin. Integer eget ligula dui. Mauris nec cursus nibh. Nunc interdum elementum leo, eu sagittis eros sodales nec. Duis dictum nulla sed tincidunt malesuada. Quisque in vulputate sapien. Sed sit amet tellus a est porta rhoncus sed eu metus. Mauris non pulvinar nisl, volutpat luctus enim. Suspendisse est nisi, sagittis at risus quis, ultricies rhoncus sem. Donec ullamcorper purus eget sapien facilisis, eu eleifend felis viverra. Suspendisse elit neque, semper aliquet neque sed, egestas tempus leo. Duis condimentum turpis duis."),
INVOCATIONS = 100000;
console.log(INVOCATIONS + " calls to calculate CRC on a " + TEST_BUFFER.length + " byte buffer...");
/**
* The input buffer for the CRC-benchmarks
* @type {Buffer}
*/
var TEST_BUFFER = new Buffer('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi mollis cursus metus vel tristique. Proin congue massa massa, a malesuada dolor ullamcorper a. Nulla eget leo vel orci venenatis placerat. Donec semper condimentum justo, vel sollicitudin dolor consequat id. Nunc sed aliquet felis, eget congue nisi. Mauris eu justo suscipit, elementum turpis ut, molestie tellus. Mauris ornare rutrum fringilla. Nulla dignissim luctus pretium. Nullam nec eros hendrerit sapien pellentesque sollicitudin. Integer eget ligula dui. Mauris nec cursus nibh. Nunc interdum elementum leo, eu sagittis eros sodales nec. Duis dictum nulla sed tincidunt malesuada. Quisque in vulputate sapien. Sed sit amet tellus a est porta rhoncus sed eu metus. Mauris non pulvinar nisl, volutpat luctus enim. Suspendisse est nisi, sagittis at risus quis, ultricies rhoncus sem. Donec ullamcorper purus eget sapien facilisis, eu eleifend felis viverra. Suspendisse elit neque, semper aliquet neque sed, egestas tempus leo. Duis condimentum turpis duis.');
// Test using SSE CRC32
start_time = Date.now();
for (var i = 0; i < INVOCATIONS; i++) {
crc = SSE4CRC32.calculate(TEST_BUFFER);
/**
* The number of times to invoke the CRC function for the benchmarks
* @type {Number}
*/
var INVOCATIONS = 100000;
console.log(INVOCATIONS + ' calls to calculate CRC on a ' + TEST_BUFFER.length + ' byte buffer...');
// Hardware-based CRC-32C
startTime = process.hrtime();
for (i = 0; i < INVOCATIONS; i++) {
crc = Sse4Crc32.calculateOnHardware(TEST_BUFFER);
}
end_time = Date.now();
console.log("\tSSE4.2 based CRC32: " + (end_time - start_time) + "ms.");
endTime = process.hrtime(startTime);
console.log('\tHardware-based CRC-32C: %d ms', (endTime[0] * 1e9 + endTime[1]) / (1000 * 1000));
// Test using pure JS CRC32 (table-based)
start_time = Date.now();
for (var i = 0; i < INVOCATIONS; i++) {
crc = js_crc32(TEST_BUFFER, false);
// Software-based CRC-32C
startTime = process.hrtime();
for (i = 0; i < INVOCATIONS; i++) {
crc = Sse4Crc32.calculateInSoftware(TEST_BUFFER);
}
end_time = Date.now();
console.log("\tPure JS based CRC32 (table-based): " + (end_time - start_time) + "ms.");
endTime = process.hrtime(startTime);
console.log('\tSoftware-based CRC-32C: %d ms', (endTime[0] * 1e9 + endTime[1]) / (1000 * 1000));
// Test using pure JS CRC32 (direct)
start_time = Date.now();
for (var i = 0; i < INVOCATIONS; i++) {
crc = js_crc32(TEST_BUFFER, true);
// Pure JS CRC-32C (table-based)
startTime = process.hrtime();
for (i = 0; i < INVOCATIONS; i++) {
crc = jsCrc32(TEST_BUFFER, false);
}
end_time = Date.now();
console.log("\tPure JS based CRC32 (direct): " + (end_time - start_time) + "ms.\n");
endTime = process.hrtime(startTime);
console.log('\tPure JS CRC-32C (table-based): %d ms', (endTime[0] * 1e9 + endTime[1]) / (1000 * 1000));
// Pure JS CRC-32C (direct)
startTime = process.hrtime();
for (i = 0; i < INVOCATIONS; i++) {
crc = jsCrc32(TEST_BUFFER, true);
}
endTime = process.hrtime(startTime);
console.log('\tPure JS CRC-32C (direct): %d ms', (endTime[0] * 1e9 + endTime[1]) / (1000 * 1000));

@@ -1,53 +0,90 @@

var SSE4CRC32 = require("../sse4_crc32"),
js_crc32 = require("crc32"),
crc, start_time, end_time;
/**
* @file Benchmarks using multiple random-length buffers
*
* @author Anand Suresh <anand.suresh@gmail.com>
* @copyright Copyright (C) 2007-2015 Voxer LLC. All rights reserved.
* @license MIT
*/
var Sse4Crc32 = require('../sse4_crc32');
var jsCrc32 = require('crc32');
var crc, startTime, endTime, i;
var MAX_STRING_LENGTH = 4096,
VALID_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()+_=-[]{}\\|;':\",./<>?`~ ",
INVOCATIONS = 100000,
TEST_BUFFERS = [],
avg_len = 0;
// Generate random strings
console.log(INVOCATIONS + " calls to calculate CRC on random length buffers upto " + MAX_STRING_LENGTH + " bytes long...");
for (var i = 0; i < INVOCATIONS; i++) {
var len = Math.random() * MAX_STRING_LENGTH,
str = "";
/**
* Maximum length of a test buffer
* @type {Number}
*/
var MAX_STRING_LENGTH = 4096;
/**
* A list of valid characters used to compose the test buffers
* @type {String}
*/
var VALID_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()+_=-[]{}\\|;\':",./<>?`~ ';
/**
* The number of times to invoke the CRC function for the benchmarks
* @type {Number}
*/
var INVOCATIONS = 100000;
// Generate the test buffers
var testBuffers = [];
var avgBufferLength = 0;
console.log(INVOCATIONS + ' calls to calculate CRC on random-length buffers upto ' + MAX_STRING_LENGTH + ' bytes long...');
for (i = 0; i < INVOCATIONS; i++) {
var len = Math.random() * MAX_STRING_LENGTH;
var str = '';
for (var j = 0; j < len; j++) {
str += VALID_CHARS.charAt(Math.random() * VALID_CHARS.length);
}
TEST_BUFFERS.push(new Buffer(str));
avg_len += len;
testBuffers.push(new Buffer(str));
avgBufferLength += len;
}
avg_len /= INVOCATIONS;
console.log("\tAvg. buffer length: " + parseInt(avg_len) + " bytes");
avgBufferLength /= INVOCATIONS;
console.log('\tAvg. buffer length: %d bytes', parseInt(avgBufferLength));
// Test using SSE CRC32
start_time = Date.now();
TEST_BUFFERS.forEach(function (str) {
crc = SSE4CRC32.calculate(str);
// Hardware-based CRC-32C
startTime = process.hrtime();
testBuffers.forEach(function(str) {
crc = Sse4Crc32.calculateOnHardware(str);
});
end_time = Date.now();
console.log("\tSSE4.2 based CRC32: " + (end_time - start_time) + "ms.");
endTime = process.hrtime(startTime);
console.log('\tHardware-based CRC-32C: %d ms', (endTime[0] * 1e9 + endTime[1]) / (1000 * 1000));
// Test using pure JS CRC32 (table-based)
start_time = Date.now();
TEST_BUFFERS.forEach(function (str) {
crc = js_crc32(str, false);
// Software-based CRC-32C
startTime = process.hrtime();
testBuffers.forEach(function(str) {
crc = Sse4Crc32.calculateInSoftware(str);
});
end_time = Date.now();
console.log("\tPure JS based CRC32 (table-based): " + (end_time - start_time) + "ms.");
endTime = process.hrtime(startTime);
console.log('\tSoftware-based CRC-32C: %d ms', (endTime[0] * 1e9 + endTime[1]) / (1000 * 1000));
// Test using pure JS CRC32 (direct)
start_time = Date.now();
TEST_BUFFERS.forEach(function (str) {
crc = js_crc32(str, true);
// Pure JS CRC-32C (table-based)
startTime = process.hrtime();
testBuffers.forEach(function(str) {
crc = jsCrc32(str, false);
});
end_time = Date.now();
console.log("\tPure JS based CRC32 (direct): " + (end_time - start_time) + "ms.\n");
endTime = process.hrtime(startTime);
console.log('\tPure JS CRC-32C (table-based): %d ms', (endTime[0] * 1e9 + endTime[1]) / (1000 * 1000));
// Pure JS CRC-32C (direct)
startTime = process.hrtime();
testBuffers.forEach(function(str) {
crc = jsCrc32(str, true);
});
endTime = process.hrtime(startTime);
console.log('\tPure JS CRC-32C (direct): %d ms', (endTime[0] * 1e9 + endTime[1]) / (1000 * 1000));

@@ -1,20 +0,28 @@

var SSE4CRC32 = require("../sse4_crc32");
/**
* @file Example usage of the SSE4CRC32 library
*
* @author Anand Suresh <anand.suresh@gmail.com>
* @copyright Copyright (C) 2007-2015 Voxer LLC. All rights reserved.
* @license MIT
*/
var Sse4Crc32 = require('../sse4_crc32');
// Usage for calculating CRC32 for strings
var str = "SSE4-CRC32: A hardware accelerated CRC32 implementation for node.js";
console.log("crc(\"" + str + "\") = " + SSE4CRC32.calculate(str));
var str = 'SSE4-CRC32: A hardware accelerated CRC32 implementation for node.js';
console.log('crc("' + str + '") = ' + Sse4Crc32.calculate(str));
// Usage for calculating CRC32 for buffers
var buf = new Buffer("SSE4-CRC32: A hardware accelerated CRC32 implementation for node.js");
console.log("crc(\"" + buf + "\") = " + SSE4CRC32.calculate(buf));
var buf = new Buffer('SSE4-CRC32: A hardware accelerated CRC32 implementation for node.js');
console.log('crc("' + buf + '") = ' + Sse4Crc32.calculate(buf));
// Usage for calculating progressive CRC32
var crc32 = new SSE4CRC32.CRC32(),
array = [ "SSE4-CRC32: ", "A hardware accelerated CRC32 implementation ", "for node.js" ];
var crc32 = new Sse4Crc32.CRC32(),
array = [ 'SSE4-CRC32: ', 'A hardware accelerated CRC32 implementation ', 'for node.js' ];
array.forEach(function (str) {
crc32.update(str);
});
console.log("crc([" + array + "]) = " + crc32.crc());
console.log('crc([' + array + ']) = ' + crc32.crc());

@@ -13,9 +13,5 @@ {

],
"version": "2.1.2",
"version": "3.0.0",
"author": "Anand Suresh <anandsuresh@gmail.com> (https://github.com/anandsuresh)",
"licenses": [
{
"type": "MIT"
}
],
"license":"MIT",
"repository": {

@@ -35,3 +31,3 @@ "type": "git",

"bindings": "~1.2.1",
"nan": "^1.6.0"
"nan": "^1.7.0"
},

@@ -38,0 +34,0 @@ "devDependencies": {

# SSE4-CRC32
Starting with the Nehalam series, Intel processors feature the Streaming SIMD
Extensions instruction set which provide a hardware-accelerated version of
the CRC32 algorithm (Castagnoli variant). This library uses the Intel SSE 4.2
instruction set to provide a fast CRC-32 algorithm.
Starting with the Nehalam series, Intel processors feature the Streaming SIMD Extensions instruction set which
provide a hardware-accelerated version of the CRC32 algorithm (Castagnoli variant). This library uses the
Intel SSE 4.2 instruction set to provide a fast CRC-32 algorithm.

@@ -11,5 +10,5 @@

- Intel Streaming SIMD Extensions 4.2 based hardware accelerated CRC 32
calculation
- Progressive CRC32 calculation
- Intel Streaming SIMD Extensions 4.2 based hardware accelerated CRC-32C calculation
- Graceful fallback to software-based CRC (table-based CRC calculation)
- Progressive CRC-32C calculation
- Supports Node.js buffers

@@ -20,26 +19,24 @@

The tests were run on a Macbook Air running an Intel Core i7 processor, with 8GB
of RAM and used buffers instead of strings to prevent having items on the V8
heap that might cause the garbage collector to fire frequently and interfere
with the test run-times.
The tests were run on a Macbook Air running an Intel Core i7 processor, with 8GB of RAM and used buffers
instead of strings to prevent having items on the V8 heap that might cause the garbage collector to fire
frequently and interfere with the test run-times.
Below are the results from the 2 test cases:
>node benchmark/1.single_1kb_length_buffer.benchmark.js
> node benchmark/1.single_1kb_length_buffer.benchmark.js
100000 calls to calculate CRC on a 1024 byte buffer...
SSE4.2 based CRC32: 26ms.
Pure JS based CRC32 (table-based): 699ms.
Pure JS based CRC32 (direct): 3704ms.
Hardware-based CRC-32C: 27.388368 ms
Software-based CRC-32C: 87.832587 ms
Pure JS CRC-32C (table-based): 726.95698 ms
Pure JS CRC-32C (direct): 4332.993692 ms
>node benchmark/2.multi_random_length_buffer.benchmark.js
100000 calls to calculate CRC on random length buffers upto 4096 bytes long...
Avg. buffer length: 2042 bytes
SSE4.2 based CRC32: 62ms.
Pure JS based CRC32 (table-based): 1968ms.
Pure JS based CRC32 (direct): 8220ms.
> node benchmark/2.multi_random_length_buffer.benchmark.js
100000 calls to calculate CRC on random-length buffers upto 4096 bytes long...
Avg. buffer length: 2049 bytes
Hardware-based CRC-32C: 88.440507 ms
Software-based CRC-32C: 173.136638 ms
Pure JS CRC-32C (table-based): 2131.861339 ms
Pure JS CRC-32C (direct): 9006.901272 ms
The results clearly show that the SSE4_CRC32 library is about **31.74** times
faster than the pure JS library!
## Installation

@@ -49,3 +46,3 @@

npm install sse4_crc32
npm install sse4_crc32

@@ -57,24 +54,30 @@

var SSE4CRC32 = require("sse4_crc32");
var Sse4Crc32 = require("sse4_crc32");
To calculate the 32-bit CRC for any string, simply use the following code:
var crc = SSE4CRC32.calculate("my string");
var crc = Sse4Crc32.calculate("my string");
Instead of passing in a string, a buffer can be passed to the `calculate()` function.
To calculate CRC in a progressive manner, use the code snippet:
Furthermore, the `calculate()` function takes an optional `initialCrc` value as the second argument, allowing
for progressive CRC calculation.
var sse4crc32 = new SSE4CRC32.CRC32(),
my_inputs = [ "some string", new Buffer("a buffer"), "yet another string", new Buffer("yet another buffer") ],
crc;
var crc = Sse4Crc32.calculate("my string");
var newCrc = Sse4Crc32.calculate("my new string", crc);
my_inputs.forEach(function (string) {
crc = sse4crc32.update(string);
});
Another way to calculate CRC in a progressive manner is as follows:
crc = sse4crc32.crc(); // The .crc() method can also be used to get the final CRC
var sse4crc32 = new Sse4Crc32.CRC32();
var myInputs = [ "some string", new Buffer("a buffer"), "yet another string", new Buffer("yet another buffer") ];
var crc;
myInputs.forEach(function (string) {
sse4crc32.update(string);
});
crc = sse4crc32.crc();
Also see the example code in the [examples](https://github.com/anandsuresh/sse4_crc32/tree/master/examples)
directory.
directory.

@@ -85,5 +88,5 @@ ## How to compile

make all // Builds the release version of the library and runs all tests
make debug // Builds the debug version of the library
make clean // Removes all files generated by builds
make all // Builds the release version of the library and runs all tests
make debug // Builds the debug version of the library
make clean // Removes all files generated by builds

@@ -90,0 +93,0 @@

@@ -1,20 +0,69 @@

var sse4_crc32 = require("bindings")("sse4_crc32");
/**
* @file Provides hardware-based CRC-32C calculation functionality, with software fallback
*
* @author Anand Suresh <anand.suresh@gmail.com>
* @copyright Copyright (C) 2007-2015 Voxer LLC. All rights reserved.
* @license MIT
*/
var Sse4Crc32 = require("bindings")("sse4_crc32");
/**
* Defines a progressive 32-bit CRC calculator
* Calculates CRC in software mode
*
* @param input The input string for which the CRC is to be calculated
* @param initial_crc The initial CRC passed in [optional]
* @param {String|Buffer} input The input string/buffer for which the CRC is to be calculated
* @param {Number} [initialCrc=0] An optional initial CRC
* @returns {Number}
*/
function swCrc32c(input, initialCrc) {
return Sse4Crc32.swCrc(input, initialCrc || 0);
}
/**
* Calculates CRC in hardware mode
*
* @param {String|Buffer} input The input string/buffer for which the CRC is to be calculated
* @param {Number} [initialCrc=0] An optional initial CRC
* @returns {Number}
*/
function hwCrc32c(input, initialCrc) {
if (!Sse4Crc32.isHardwareCrcSupported) throw new Error('Hardware CRC-32C not supported!');
return Sse4Crc32.hwCrc(input, initialCrc || 0);
}
/**
* Used to calculate 32-bit CRC for single instances of strings and/or buffers
*
* This function automatically determines the best mode of operation (h/w vs. s/w implementation).
*
* @param {String|Buffer} input The input string for which the CRC is to be calculated
* @param {Number} [initialCrc=0] An optional initial CRC
* @returns {Number}
*/
function calculate(input, initialCrc) {
var crcFunction = Sse4Crc32.isHardwareCrcSupported ? Sse4Crc32.hwCrc : Sse4Crc32.swCrc;
return crcFunction(input, initialCrc || 0);
}
/**
* Defines a progressive CRC-32C calculator
*
* @param {String|Buffer} input The input string for which the CRC is to be calculated
* @param {Number} [initialCrc=0] An optional initial CRC
* @constructor
*/
function CRC32(input, initial_crc) {
this.crc32 = initial_crc || 0;
function Crc32C(input, initialCrc) {
this.crc32c = initialCrc || 0;
if (input) {
this.update(input);
}
if (input) this.update(input);
}
/**

@@ -24,15 +73,17 @@ * Progressively calculates the 32-bit CRC

* @param input Additional input to calculate the CRC for
* @returns {Crc32C}
*/
CRC32.prototype.update = function (input) {
this.crc32 = sse4_crc32.calculate(input, this.crc32);
return this.crc32;
Crc32C.prototype.update = function(input) {
this.crc32c = calculate(input, this.crc32c);
return this;
};
/**
* Returns the 32-bit CRC
*
* @returns {Integer}
* @returns {Number}
*/
CRC32.prototype.crc = function () {
return this.crc32;
Crc32C.prototype.crc = function() {
return this.crc32c;
};

@@ -42,17 +93,12 @@

/**
* Used to calculate 32-bit CRC for single instances of strings and/or buffers
*
* @param input The input string for which the CRC is to be calculated
* @param initial_crc The initial CRC passed in [optional]
*
* @returns {Integer}
* Export the required functions/classes
* @type {Object}
*/
function calculate(input, initial_crc) {
return sse4_crc32.calculate(input, initial_crc || 0);
}
module.exports = {
isHardwareCrcSupported: Sse4Crc32.isHardwareCrcSupported,
calculateInSoftware : swCrc32c,
calculateOnHardware : hwCrc32c,
module.exports = {
CRC32 : CRC32,
CRC32 : Crc32C,
calculate: calculate
};

@@ -1,3 +0,11 @@

var SSE4CRC32 = require("../sse4_crc32"),
tap = require("tap"),
/**
* @file Benchmarks using multiple random-length buffers
*
* @author Anand Suresh <anand.suresh@gmail.com>
* @copyright Copyright (C) 2007-2015 Voxer LLC. All rights reserved.
* @license MIT
*/
var SSE4CRC32 = require('../sse4_crc32'),
tap = require('tap'),
test = tap.test;

@@ -8,11 +16,11 @@

{
input : "SSE4-CRC32: A hardware accelerated CRC32 implementation for node.js",
input : 'SSE4-CRC32: A hardware accelerated CRC32 implementation for node.js',
output: 3039989317
},
{
input : new Buffer("SSE4-CRC32: A hardware accelerated CRC32 implementation for node.js"),
input : new Buffer('SSE4-CRC32: A hardware accelerated CRC32 implementation for node.js'),
output: 3039989317
},
{
input : [ "SSE4-CRC32: ", "A hardware accelerated CRC32 implementation ", "for node.js" ],
input : ['SSE4-CRC32: ', 'A hardware accelerated CRC32 implementation ', 'for node.js'],
output: 3039989317

@@ -26,12 +34,12 @@ }

*/
test("Basic Tests", function (t) {
TEST_CASES.forEach(function (test_case) {
test('Basic Tests', function(t) {
TEST_CASES.forEach(function(test_case) {
if (Array.isArray(test_case.input)) {
var test_crc = new SSE4CRC32.CRC32();
test_case.input.forEach(function (str) {
test_case.input.forEach(function(str) {
test_crc.update(str);
});
t.equals(test_crc.crc(), test_case.output, "CRC32 for [ " + test_case.input + " ] should be " + test_case.output);
t.equals(test_crc.crc(), test_case.output, 'CRC32 for [ ' + test_case.input + ' ] should be ' + test_case.output);
} else {
t.equals(SSE4CRC32.calculate(test_case.input), test_case.output, "CRC32 for " + test_case.input + " should be " + test_case.output);
t.equals(SSE4CRC32.calculate(test_case.input), test_case.output, 'CRC32 for ' + test_case.input + ' should be ' + test_case.output);
}

@@ -38,0 +46,0 @@ });

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