Socket
Socket
Sign inDemoInstall

memcpy

Package Overview
Dependencies
1
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.3.0 to 0.4.0

2

package.json
{
"name": "memcpy",
"version": "0.3.0",
"version": "0.4.0",
"author": "Daniel Wirtz <dcode@dcode.io>",

@@ -5,0 +5,0 @@ "description": "Copies data between node Buffers and/or ArrayBuffers up to ~75 times faster than in pure JS.",

@@ -12,20 +12,31 @@ node-memcpy

```
i memcpy.100k > cc Buffer -> Buffer: 22.222ms
i memcpy.100k > cc Buffer -> ArrayBuffer: 23.548ms
i memcpy.100k > cc ArrayBuffer -> Buffer: 22.108ms
i memcpy.100k > cc ArrayBuffer -> ArrayBuffer: 21.778ms
// C++ binding
i memcpy.100k > js Buffer -> Buffer: 23.163ms
i memcpy.100k > js Buffer -> ArrayBuffer: 1043.713ms
i memcpy.100k > js ArrayBuffer -> Buffer: 1003.351ms
i memcpy.100k > js ArrayBuffer -> ArrayBuffer: 1607.978ms
i memcpy.100k > cc Buffer -> Buffer: 22.756ms
i memcpy.100k > cc Buffer -> ArrayBuffer: 23.861ms
i memcpy.100k > cc Buffer -> Uint8Array: 22.953ms
i memcpy.100k > cc ArrayBuffer -> Buffer: 22.955ms
i memcpy.100k > cc ArrayBuffer -> ArrayBuffer: 23.273ms
i memcpy.100k > cc ArrayBuffer -> Uint8Array: 22.685ms
i memcpy.100k > cc Uint8Array -> Buffer: 23.472ms
i memcpy.100k > cc Uint8Array -> ArrayBuffer: 22.975ms
i memcpy.100k > cc Uint8Array -> Uint8Array: 22.953ms
// Native JS
i memcpy.100k > js Buffer -> Buffer: 21.617ms
i memcpy.100k > js Buffer -> ArrayBuffer: 993.361ms
i memcpy.100k > js Buffer -> Uint8Array: 410.010ms
i memcpy.100k > js ArrayBuffer -> Buffer: 940.273ms
i memcpy.100k > js ArrayBuffer -> ArrayBuffer: 1626.182ms
i memcpy.100k > js ArrayBuffer -> Uint8Array: 1084.790ms
i memcpy.100k > js Uint8Array -> Buffer: 386.218ms
i memcpy.100k > js Uint8Array -> ArrayBuffer: 1107.530ms
i memcpy.100k > js Uint8Array -> Uint8Array: 502.653ms
```
Breakdown
---------
* `Buffer` to `Buffer` is about as fast as using `Buffer#copy`
* `Buffer` to `ArrayBuffer` is about **45 times faster**
* `ArrayBuffer` to `Buffer` is about **45 times faster**
* `ArrayBuffer` to `ArrayBuffer` is about **75 times faster**
API

@@ -35,11 +46,13 @@ ---

| Argument | Type | Optional | Description
|--------------|-------------------------|-----------|------------------------------------
| target | Buffer&#124;ArrayBuffer | | Target buffer to copy to
| targetStart | number | omittable | Target offset to begin copying to
| source | Buffer&#124;ArrayBuffer | | Source buffer to copy from
| sourceStart | number | optional | Source offset to begin copying from
| sourceEnd | number | optional | Source offset to end copying from
| **@returns** | number | | Number of bytes copied
| Argument | Type | Optional | Description
|--------------|---------------------------|-----------|------------------------------------------------------------------
| target | Buffer &#124; ArrayBuffer &#124; Uint8Array | | Target buffer to copy to
| targetStart | number | omittable | Target offset to begin copying to, defaults to `0`
| source | Buffer &#124; ArrayBuffer &#124; Uint8Array | | Source buffer to copy from
| sourceStart | number | optional | Source offset to begin copying from, defaults to `0`
| sourceEnd | number | optional | Source offset to end copying from, defaults ot `source.length`
| **@returns** | number | | Number of bytes copied
Source and target regions may overlap.
Usage

@@ -57,5 +70,5 @@ -----

Please keep in mind that - besides the nice numbers - this is still to be considered experimental. I'd love if you'd
review the C++ code to validate that it's safe. I can't yet think about a sane use case, though, as just sticking with
review the C++ code to validate that it's safe. I can't yet think of a sane use case, though, as just sticking with
Buffers on node.js and ArrayBuffers in the browser should be best practice.
**License:** [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html)

@@ -64,5 +64,5 @@ /*

* @name memcpy
* @param {!(Buffer|ArrayBuffer)} target Destination
* @param {!(Buffer|ArrayBuffer|Uint8Array)} target Destination
* @param {number|!(Buffer|ArrayBuffer)} targetStart Destination start, defaults to 0.
* @param {(!(Buffer|ArrayBuffer)|number)=} source Source
* @param {(!(Buffer|ArrayBuffer|Uint8Array)|number)=} source Source
* @param {number=} sourceStart Source start, defaults to 0.

@@ -92,6 +92,3 @@ * @param {number=} sourceEnd Source end, defaults to capacity.

// Slower:
// for (i=sourceStart, j=targetStart; i<sourceEnd; ++i, ++j) target[j] = source[i];
// ArrayBuffer source -> Buffer target (the binding is about 45 times faster)
// ArrayBuffer|Uint8Array source -> Buffer target (the binding is about 45 times faster)
} else {

@@ -103,3 +100,5 @@ sourceEnd = sourceEnd || source.byteLength;

}
for (i=sourceStart, j=targetStart, k=new Uint8Array(source); i<sourceEnd; ++i, ++j) target[j] = k[i];
for (i=sourceStart, j=targetStart,
k=source instanceof Uint8Array ? source : new Uint8Array(source);
i<sourceEnd; ++i, ++j) target[j] = k[i];
k = null;

@@ -110,3 +109,3 @@ }

// Buffer source -> ArrayBuffer target (the binding is about 45 times faster)
// Buffer source -> ArrayBuffer|Uint8Array target (the binding is about 45 times faster)
if (source instanceof Buffer) {

@@ -118,6 +117,8 @@ sourceEnd = sourceEnd || source.length;

}
for (i=sourceStart, j=targetStart, k=new Uint8Array(target); i<sourceEnd; ++i, ++j) k[j] = source[i];
for (i=sourceStart, j=targetStart,
k=target instanceof Uint8Array ? target : new Uint8Array(target);
i<sourceEnd; ++i, ++j) k[j] = source[i];
k = null;
// ArrayBuffer source -> ArrayBuffer target (the binding is about 75 times faster)
// ArrayBuffer|Uint8Array source -> ArrayBuffer|Uint8Array target (the binding is up to about 75 times faster)
} else {

@@ -129,3 +130,6 @@ sourceEnd = sourceEnd || source.byteLength;

}
for (i=sourceStart, j=targetStart, k=new Uint8Array(target), l=new Uint8Array(source); i<sourceEnd; ++i, ++j) k[j] = l[i];
for (i=sourceStart, j=targetStart,
k=target instanceof Uint8Array ? target : new Uint8Array(target),
l=source instanceof Uint8Array ? source : new Uint8Array(source);
i<sourceEnd; ++i, ++j) k[j] = l[i];
k = l = null;

@@ -132,0 +136,0 @@ }

@@ -29,4 +29,4 @@ function fill(buf) {

[memcpy.binding, memcpy.native].forEach(function(memcpy) {
[Buffer, ArrayBuffer].forEach(function(Type1) {
[Buffer, ArrayBuffer].forEach(function(Type2) {
[Buffer, ArrayBuffer, Uint8Array].forEach(function(Type1) {
[Buffer, ArrayBuffer, Uint8Array].forEach(function(Type2) {
var b1 = new Type1(8),

@@ -56,3 +56,3 @@ b2 = new Type2(8);

[memcpy.binding, memcpy.native].forEach(function(memcpy) {
[Buffer, ArrayBuffer].forEach(function(Type) {
[Buffer, ArrayBuffer, Uint8Array].forEach(function(Type) {
var b = new Type(8);

@@ -80,4 +80,4 @@ test.log((memcpy === memcpy.binding ? "cc".cyan : "js".green)+" "+b.constructor.name);

[memcpy.binding, memcpy.native].forEach(function(memcpy) {
[Buffer, ArrayBuffer].forEach(function(Type1) {
[Buffer, ArrayBuffer].forEach(function(Type2) {
[Buffer, ArrayBuffer, Uint8Array].forEach(function(Type1) {
[Buffer, ArrayBuffer, Uint8Array].forEach(function(Type2) {
var b1 = new Type1(1024),

@@ -84,0 +84,0 @@ b2 = new Type2(1024);

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc