8
0

position.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals window, document, setTimeout */
  6. document.getElementById( 'run' ).addEventListener( 'click', () => {
  7. log( 'Running tests...' );
  8. setTimeout( async () => {
  9. await runTest( 'concat', testConcat );
  10. await runTest( 'spread operator', testSpread );
  11. await runTest( 'for-loop', testForLoop );
  12. await runTest( 'ultra-loop', testUltraForLoop );
  13. log( 'done' );
  14. } );
  15. } );
  16. window.cache = [];
  17. const output = document.getElementById( 'output' );
  18. function log( line ) {
  19. const paragraphElement = document.createElement( 'p' );
  20. paragraphElement.innerText = line;
  21. output.appendChild( paragraphElement );
  22. }
  23. function runTest( name, callback ) {
  24. return new Promise( resolve => {
  25. const start = new Date();
  26. const repetitions = 10000000;
  27. const root = {
  28. root: 'foo',
  29. path: [ 0 ]
  30. };
  31. const path = [ 0, 2 ];
  32. for ( let i = 0; i < repetitions; i++ ) {
  33. const newPath = callback( root, path );
  34. window.cache.push( newPath.length );
  35. }
  36. const end = new Date();
  37. log( ` > ${ name } took ${ end - start }ms` );
  38. setTimeout( () => {
  39. resolve();
  40. }, 50 );
  41. } );
  42. }
  43. class PositionConcat {
  44. constructor( root, path, stickiness = 'left' ) {
  45. if ( !( path instanceof Array ) || path.length === 0 ) {
  46. throw new Error( 'model-position-path-incorrect-format' );
  47. }
  48. path = root.path.concat( path );
  49. root = root.root;
  50. this.root = root;
  51. this.path = path;
  52. this.stickiness = stickiness;
  53. }
  54. }
  55. class PositionSpread {
  56. constructor( root, path, stickiness = 'left' ) {
  57. if ( !( path instanceof Array ) || path.length === 0 ) {
  58. throw new Error( 'model-position-path-incorrect-format' );
  59. }
  60. path = [ ...root.path, ...path ];
  61. root = root.root;
  62. this.root = root;
  63. this.path = path;
  64. this.stickiness = stickiness;
  65. }
  66. }
  67. class PositionForLoop {
  68. constructor( root, path, stickiness = 'left' ) {
  69. if ( !( path instanceof Array ) || path.length === 0 ) {
  70. throw new Error( 'model-position-path-incorrect-format' );
  71. }
  72. path = forLoop( root.path, path );
  73. root = root.root;
  74. this.root = root;
  75. this.path = path;
  76. this.stickiness = stickiness;
  77. }
  78. }
  79. class PositionUltraForLoop {
  80. constructor( root, path, stickiness = 'left' ) {
  81. if ( !( path instanceof Array ) || path.length === 0 ) {
  82. throw new Error( 'model-position-path-incorrect-format' );
  83. }
  84. path = ultraForLoop( root.path, path );
  85. root = root.root;
  86. this.root = root;
  87. this.path = path;
  88. this.stickiness = stickiness;
  89. }
  90. }
  91. function testConcat( root, path ) {
  92. return new PositionConcat( root, path );
  93. }
  94. function testSpread( root, path ) {
  95. return new PositionSpread( root, path );
  96. }
  97. function testForLoop( root, path ) {
  98. return new PositionForLoop( root, path );
  99. }
  100. function testUltraForLoop( root, path ) {
  101. return new PositionUltraForLoop( root, path );
  102. }
  103. function forLoop( rootPath, path ) {
  104. const newPath = [];
  105. for ( let i = 0; i < rootPath.length; i++ ) {
  106. newPath.push( rootPath[ i ] );
  107. }
  108. for ( let i = 0; i < path.length; i++ ) {
  109. newPath.push( path[ i ] );
  110. }
  111. return newPath;
  112. }
  113. function ultraForLoop( rootPath, path ) {
  114. const fullLength = rootPath.length + path.length;
  115. const newPath = new Array( fullLength );
  116. for ( let i = 0; i < rootPath.length; i++ ) {
  117. newPath[ i ] = rootPath[ i ];
  118. }
  119. for ( let i = 0; i < path.length; i++ ) {
  120. newPath[ rootPath.length + i ] = path[ i ];
  121. }
  122. return newPath;
  123. }