Advertisement
lignite0

ROBO24 - benchmark getter test

Oct 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export function ReadProperty() {
  2.     const object = {
  3.         x: 5,
  4.     };
  5.  
  6.     return function () {
  7.         return object.x;
  8.     };
  9. }
  10.  
  11. export function ReadArrayBuffer() {
  12.  
  13.     const buffer = new ArrayBuffer(4096);
  14.     const dataView = new DataView(buffer, 2048);
  15.     dataView.setInt16(16, 5);
  16.  
  17.     return function () {
  18.         return dataView.getInt16(16);
  19.     };
  20. }
  21.  
  22. export function DefineProperty() {
  23.  
  24.     const buffer = new ArrayBuffer(4096);
  25.     const dataView = new DataView(buffer, 2048);
  26.     dataView.setInt16(16, 5);
  27.  
  28.     const object = {
  29.         dataView,
  30.     };
  31.  
  32.     Object.defineProperty(object, 'x', {
  33.         get: function () {
  34.             return this.dataView.getInt16(16);
  35.         },
  36.     });
  37.  
  38.     return function () {
  39.         return object.x;
  40.     };
  41. }
  42.  
  43. export function DefinePropertyClass() {
  44.  
  45.     const buffer = new ArrayBuffer(4096);
  46.     const dataView = new DataView(buffer, 2048);
  47.     dataView.setInt16(16, 5);
  48.  
  49.     class Chunk {
  50.         constructor(dataView) {
  51.             this.dataView = dataView;
  52.         }
  53.     }
  54.  
  55.     Chunk.prototype.decoder = {
  56.         fields: [
  57.             {field: "x", offset: 16, type: "Int16"},
  58.             {field: "y", offset: 2, type: "Int16"},
  59.         ],
  60.     };
  61.  
  62.     const chunk = new Chunk(dataView);
  63.  
  64.     for (let {field, offset, type} of chunk.decoder.fields) {
  65.         Object.defineProperty(chunk, field, {
  66.             get: function () {
  67.                 return this.dataView[`get${type}`](offset);
  68.             },
  69.         });
  70.     }
  71.  
  72.     return function () {
  73.         return chunk.x;
  74.     };
  75. }
  76.  
  77. export function ArrayBufferWithProxy() {
  78.  
  79.     const buffer = new ArrayBuffer(4096);
  80.     const dataView = new DataView(buffer, 2048);
  81.  
  82.     class Chunk {
  83.         constructor(dataView) {
  84.             this.dataView = dataView;
  85.         }
  86.     }
  87.  
  88.     Chunk.prototype.cache = {};
  89.     Chunk.prototype.decoder = {
  90.         "x": {offset: 0, type: "Int16"},
  91.         "y": {offset: 2, type: "Int16"},
  92.     };
  93.  
  94.     const chunk = new Proxy(new Chunk(dataView), {
  95.         get: function (target, property, receiver) {
  96.             if (target[property] !== undefined) {
  97.                 return target[property];
  98.             }
  99.  
  100.             if (target['cache'][property] === undefined) {
  101.                 const readerInfo = target['decoder'][property];
  102.                 if (readerInfo === undefined) {
  103.                     return undefined;
  104.                 }
  105.                 const {offset, type} = readerInfo;
  106.                 const {dataView} = target;
  107.                 const getter = dataView[`get${type}`];
  108.                 const value = getter.call(dataView, offset);
  109.             }
  110.  
  111.             return target['cache'][property];
  112.         }
  113.     });
  114.  
  115.     return function () {
  116.         return chunk.x;
  117.     };
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement