time-to-botec

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

README.md (2893B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2019 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 # Define Memoized Property
     22 
     23 > [Define][mdn-define-property] a memoized object property.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var defineMemoizedProperty = require( '@stdlib/utils/define-memoized-property' );
     31 ```
     32 
     33 #### defineMemoizedProperty( obj, prop, descriptor )
     34 
     35 [Defines][mdn-define-property] a memoized object property.
     36 
     37 ```javascript
     38 var obj = {};
     39 
     40 function foo() {
     41     return 'bar';
     42 }
     43 
     44 defineMemoizedProperty( obj, 'foo', {
     45     'configurable': false,
     46     'enumerable': true,
     47     'writable': false,
     48     'value': foo
     49 });
     50 
     51 var v = obj.foo;
     52 // returns 'bar'
     53 ```
     54 
     55 A property `descriptor` has the following required properties:
     56 
     57 -   **value**: synchronous function whose return value will be memoized and set as the property value.
     58 
     59 A property `descriptor` has the following optional properties:
     60 
     61 -   **configurable**: `boolean` indicating if property descriptor can be changed and if the property can be deleted from the provided object. Default: `false`.
     62 -   **enumerable**: `boolean` indicating if the property shows up when enumerating object properties. Default: `false`.
     63 -   **writable**: `boolean` indicating if the value associated with the property can be changed with an assignment operator. Default: `false`.
     64 
     65 </section>
     66 
     67 <!-- /.usage -->
     68 
     69 <section class="notes">
     70 
     71 ## Notes
     72 
     73 -   Until deferred evaluation, an object property is **configurable**.
     74 
     75 </section>
     76 
     77 <!-- /.notes -->
     78 
     79 <section class="examples">
     80 
     81 ## Examples
     82 
     83 <!-- eslint no-undef: "error" -->
     84 
     85 ```javascript
     86 var fibonacci = require( '@stdlib/math/base/special/fibonacci' );
     87 var defineMemoizedProperty = require( '@stdlib/utils/define-memoized-property' );
     88 
     89 function Foo() {
     90     var self;
     91     if ( !(this instanceof Foo) ) {
     92         return new Foo();
     93     }
     94     self = this;
     95     this.count = 0;
     96     defineMemoizedProperty( this, 'fibo', {
     97         'value': fibo
     98     });
     99     return this;
    100 
    101     function fibo() {
    102         self.count += 1;
    103         return fibonacci( 73 );
    104     }
    105 }
    106 
    107 var foo = new Foo();
    108 
    109 var i;
    110 for ( i = 0; i < 10; i++ ) {
    111     console.log( 'F: %d. Count: %d.', foo.fibo, foo.count );
    112 }
    113 ```
    114 
    115 </section>
    116 
    117 <!-- /.examples -->
    118 
    119 <section class="links">
    120 
    121 [mdn-define-property]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
    122 
    123 </section>
    124 
    125 <!-- /.links -->