8
0

position.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 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. const output = document.getElementById( 'output' );
  17. function log( line ) {
  18. const paragraphElement = document.createElement( 'p' );
  19. paragraphElement.innerText = line;
  20. output.appendChild( paragraphElement );
  21. }
  22. function runTest( name, callback ) {
  23. return new Promise( resolve => {
  24. const start = new Date();
  25. const repetitions = 10000000;
  26. const z = Array( repetitions );
  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. z[ i ] = [ 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 Position {
  44. constructor( root, path, stickiness = 'left', pathMergeMethod ) {
  45. if ( !( path instanceof Array ) || path.length === 0 ) {
  46. throw new Error( 'model-position-path-incorrect-format' );
  47. }
  48. path = pathMergeMethod( root.path, path );
  49. root = root.root;
  50. this.root = root;
  51. this.path = path;
  52. this.stickiness = stickiness;
  53. }
  54. }
  55. function testConcat( root, path ) {
  56. return new Position( root, path, 'right', concat );
  57. }
  58. function testSpread( root, path ) {
  59. return new Position( root, path, 'right', spread );
  60. }
  61. function testForLoop( root, path ) {
  62. return new Position( root, path, 'right', forLoop );
  63. }
  64. function testUltraForLoop( root, path ) {
  65. return new Position( root, path, 'right', ultraForLoop );
  66. }
  67. function concat( rootPath, path ) {
  68. return rootPath.concat( path );
  69. }
  70. function spread( rootPath, path ) {
  71. return [ ...rootPath, ...path ];
  72. }
  73. function forLoop( rootPath, path ) {
  74. const newPath = [];
  75. for ( let i = 0; i < rootPath.length; i++ ) {
  76. newPath.push( rootPath[ i ] );
  77. }
  78. for ( let i = 0; i < path.length; i++ ) {
  79. newPath.push( path[ i ] );
  80. }
  81. return newPath;
  82. }
  83. function ultraForLoop( rootPath, path ) {
  84. const fullLength = rootPath.length + path.length;
  85. const newPath = new Array( fullLength );
  86. for ( let i = 0; i < rootPath.length; i++ ) {
  87. newPath[ i ] = rootPath[ i ];
  88. }
  89. for ( let i = 0; i < path.length; i++ ) {
  90. newPath[ rootPath.length + i ] = path[ i ];
  91. }
  92. return newPath;
  93. }