time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (5140B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2018 The Stdlib Authors.
      6 
      7 Licensed under the Apache License, Version 2.0 (the "License");
      8 you may not use this file except in compliance with the License.
      9 You may obtain a copy of the License at
     10 
     11    http://www.apache.org/licenses/LICENSE-2.0
     12 
     13 Unless required by applicable law or agreed to in writing, software
     14 distributed under the License is distributed on an "AS IS" BASIS,
     15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 See the License for the specific language governing permissions and
     17 limitations under the License.
     18 
     19 -->
     20 
     21 # arraybuffer2buffer
     22 
     23 > Allocate a [buffer][@stdlib/buffer/ctor] from an [ArrayBuffer][@stdlib/array/buffer].
     24 
     25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
     26 
     27 <section class="intro">
     28 
     29 </section>
     30 
     31 <!-- /.intro -->
     32 
     33 <!-- Package usage documentation. -->
     34 
     35 <section class="usage">
     36 
     37 ## Usage
     38 
     39 ```javascript
     40 var arraybuffer2buffer = require( '@stdlib/buffer/from-arraybuffer' );
     41 ```
     42 
     43 #### arraybuffer2buffer( buf\[, byteOffset\[, length]] )
     44 
     45 Allocates a [buffer][@stdlib/buffer/ctor] from an [`ArrayBuffer`][@stdlib/array/buffer].
     46 
     47 ```javascript
     48 var ArrayBuffer = require( '@stdlib/array/buffer' );
     49 var ab = new ArrayBuffer( 10 );
     50 
     51 var buf = arraybuffer2buffer( ab );
     52 // returns <Buffer>
     53 ```
     54 
     55 By default, the function allocates a [buffer][@stdlib/buffer/ctor] starting from the first byte of the provided [`ArrayBuffer`][@stdlib/array/buffer]. To specify an alternative starting index, provide a `byteOffset`.
     56 
     57 ```javascript
     58 var ArrayBuffer = require( '@stdlib/array/buffer' );
     59 var ab = new ArrayBuffer( 10 );
     60 
     61 var buf = arraybuffer2buffer( ab, 2 );
     62 // returns <Buffer>
     63 
     64 var len = buf.length;
     65 // returns 8
     66 ```
     67 
     68 By default, the function allocates a [buffer][@stdlib/buffer/ctor] until the end of the provided [`ArrayBuffer`][@stdlib/array/buffer]. To allocate a specified number of [`ArrayBuffer`][@stdlib/array/buffer] bytes, provide a `length`.
     69 
     70 ```javascript
     71 var ArrayBuffer = require( '@stdlib/array/buffer' );
     72 var ab = new ArrayBuffer( 10 );
     73 
     74 var buf = arraybuffer2buffer( ab, 2, 4 );
     75 // returns <Buffer>
     76 
     77 var len = buf.length;
     78 // returns 4
     79 ```
     80 
     81 </section>
     82 
     83 <!-- /.usage -->
     84 
     85 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
     86 
     87 <section class="notes">
     88 
     89 ## Notes
     90 
     91 The behavior of this function varies across Node.js versions due to changes in the underlying Node.js APIs:
     92 
     93 -   `<3.0.0`: the function copies [`ArrayBuffer`][@stdlib/array/buffer] bytes to a new [`Buffer`][@stdlib/buffer/ctor] instance.
     94 -   `>=3.0.0` and `<5.10.0`: if provided a `byteOffset`, the function copies [`ArrayBuffer`][@stdlib/array/buffer] bytes to a new [`Buffer`][@stdlib/buffer/ctor] instance; otherwise, the function returns a view of an [`ArrayBuffer`][@stdlib/array/buffer] without copying the underlying memory.
     95 -   `<6.0.0`: if provided an empty [`ArrayBuffer`][@stdlib/array/buffer], the function returns an empty [`Buffer`][@stdlib/buffer/ctor] which is **not** an [`ArrayBuffer`][@stdlib/array/buffer] view.
     96 -   `>=6.0.0`: the function returns a view of an [`ArrayBuffer`][@stdlib/array/buffer] without copying the underlying memory.
     97 
     98 </section>
     99 
    100 <!-- /.notes -->
    101 
    102 <!-- Package usage examples. -->
    103 
    104 <section class="examples">
    105 
    106 ## Examples
    107 
    108 <!-- eslint no-undef: "error" -->
    109 
    110 ```javascript
    111 var ArrayBuffer = require( '@stdlib/array/buffer' );
    112 var Float64Array = require( '@stdlib/array/float64' );
    113 var randu = require( '@stdlib/random/base/randu' );
    114 var IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' );
    115 var arraybuffer2buffer = require( '@stdlib/buffer/from-arraybuffer' );
    116 
    117 var high;
    118 var view;
    119 var low;
    120 var buf;
    121 var ab;
    122 var i;
    123 
    124 // Allocate an ArrayBuffer:
    125 ab = new ArrayBuffer( 64 );
    126 
    127 // Create a Float64 view and set random values:
    128 view = new Float64Array( ab );
    129 for ( i = 0; i < view.length; i++ ) {
    130     view[ i ] = randu();
    131 }
    132 
    133 // Create a new buffer from the ArrayBuffer:
    134 buf = arraybuffer2buffer( ab );
    135 
    136 // Read the high and low words for each double:
    137 for ( i = 0; i < view.length; i++ ) {
    138     if ( IS_LITTLE_ENDIAN ) {
    139         high = buf.readUInt32LE( (8*i)+4 );
    140         low = buf.readUInt32LE( 8*i );
    141     } else {
    142         high = buf.readUInt32BE( 8*i );
    143         low = buf.readUInt32BE( (8*i)+4 );
    144     }
    145     console.log( 'Value: %d. High: %d. Low: %d.', view[ i ], high, low );
    146 }
    147 ```
    148 
    149 </section>
    150 
    151 <!-- /.examples -->
    152 
    153 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    154 
    155 <section class="references">
    156 
    157 </section>
    158 
    159 <!-- /.references -->
    160 
    161 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    162 
    163 <section class="links">
    164 
    165 [@stdlib/buffer/ctor]: https://www.npmjs.com/package/@stdlib/buffer/tree/main/ctor
    166 
    167 [@stdlib/array/buffer]: https://www.npmjs.com/package/@stdlib/array-buffer
    168 
    169 </section>
    170 
    171 <!-- /.links -->