time-to-botec

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

README.md (5002B)


      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 # prepend
     22 
     23 > Add elements from one collection to the beginning of another collection.
     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 prepend = require( '@stdlib/utils/prepend' );
     41 ```
     42 
     43 #### prepend( collection1, collection2 )
     44 
     45 Adds elements from one `collection` to the beginning of another `collection`. A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (i.e., an [`Object`][mdn-object] having a valid writable `length` property). 
     46 
     47 ```javascript
     48 var arr = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
     49 
     50 var out = prepend( arr, [ 6.0, 7.0 ] );
     51 // returns [ 6.0, 7.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
     52 
     53 var bool = ( out === arr );
     54 // returns true
     55 ```
     56 
     57 Note that the function returns the extended collection. For [typed arrays][mdn-typed-array], the returned value is a new [typed array][mdn-typed-array] view whose underlying [`ArrayBuffer`][mdn-arraybuffer] may **not** equal the underlying [`ArrayBuffer`][mdn-arraybuffer] for the input `collection`.
     58 
     59 ```javascript
     60 var ArrayBuffer = require( '@stdlib/array/buffer' );
     61 var Float64Array= require( '@stdlib/array/float64' );
     62 
     63 var buf = new ArrayBuffer( 3*8 ); // 8 bytes per double
     64 
     65 var arr = new Float64Array( buf, 8, 2 );
     66 arr[ 0 ] = 1.0;
     67 arr[ 1 ] = 2.0;
     68 
     69 var out = prepend( arr, [ 3.0 ] );
     70 // returns <Float64Array>[ 3.0, 1.0, 2.0 ]
     71 
     72 var bool = ( out === arr );
     73 // returns false
     74 
     75 bool = ( out.buffer === arr.buffer );
     76 // returns true
     77 
     78 out = prepend( out, [ 4.0 ] );
     79 // returns <Float64Array>[ 4.0, 3.0, 1.0, 2.0 ]
     80 
     81 bool = ( out.buffer === arr.buffer );
     82 // returns false
     83 ```
     84 
     85 </section>
     86 
     87 <!-- /.usage -->
     88 
     89 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
     90 
     91 <section class="notes">
     92 
     93 ## Notes
     94 
     95 -   The function adds elements to a [typed array][mdn-typed-array] by setting values in the underlying [`ArrayBuffer`][mdn-arraybuffer]. If an [`ArrayBuffer`][mdn-arraybuffer] does not have enough bytes in which to store all elements, the function allocates a new [`ArrayBuffer`][mdn-arraybuffer] capable of holding `2^n` elements, where `n` is the next power of `2`. This procedure is similar to how environments internally handle dynamic memory allocation for [`Arrays`][mdn-array].
     96 -   Beware when providing [typed arrays][mdn-typed-array] which are views pointing to a shared (or pooled) [`ArrayBuffer`][mdn-arraybuffer]. Because the function sets [`ArrayBuffer`][mdn-arraybuffer] bytes outside of a provided [view][mdn-typed-array], the function may overwrite bytes belonging to one or more external views. This could be a potential **security vulnerability**. Prefer providing [typed arrays][mdn-typed-array] which have an exclusive [`ArrayBuffer`][mdn-arraybuffer]; otherwise, be sure to plan for and guard against mutated state.
     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 Float64Array= require( '@stdlib/array/float64' );
    112 var prepend = require( '@stdlib/utils/prepend' );
    113 
    114 var arr;
    115 var i;
    116 var j;
    117 
    118 arr = new Float64Array();
    119 for ( i = 0; i < 100; i++ ) {
    120     j = i * 3;
    121     arr = prepend( arr, [ j+2, j+1, j ] );
    122 }
    123 console.log( arr );
    124 ```
    125 
    126 </section>
    127 
    128 <!-- /.examples -->
    129 
    130 <!-- 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. -->
    131 
    132 <section class="references">
    133 
    134 </section>
    135 
    136 <!-- /.references -->
    137 
    138 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    139 
    140 <section class="links">
    141 
    142 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    143 
    144 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
    145 
    146 [mdn-arraybuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
    147 
    148 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
    149 
    150 </section>
    151 
    152 <!-- /.links -->