...

Source file src/github.com/PuerkitoBio/goquery/bench_traversal_test.go

Documentation: github.com/PuerkitoBio/goquery

     1  package goquery
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/andybalholm/cascadia"
     7  )
     8  
     9  func BenchmarkFind(b *testing.B) {
    10  	var n int
    11  
    12  	for i := 0; i < b.N; i++ {
    13  		if n == 0 {
    14  			n = DocB().Find("dd").Length()
    15  
    16  		} else {
    17  			DocB().Find("dd")
    18  		}
    19  	}
    20  	if n != 41 {
    21  		b.Fatalf("want 41, got %d", n)
    22  	}
    23  }
    24  
    25  func BenchmarkFindWithinSelection(b *testing.B) {
    26  	var n int
    27  
    28  	b.StopTimer()
    29  	sel := DocW().Find("ul")
    30  	b.StartTimer()
    31  	for i := 0; i < b.N; i++ {
    32  		if n == 0 {
    33  			n = sel.Find("a[class]").Length()
    34  		} else {
    35  			sel.Find("a[class]")
    36  		}
    37  	}
    38  	if n != 39 {
    39  		b.Fatalf("want 39, got %d", n)
    40  	}
    41  }
    42  
    43  func BenchmarkFindSelection(b *testing.B) {
    44  	var n int
    45  
    46  	b.StopTimer()
    47  	sel := DocW().Find("ul")
    48  	sel2 := DocW().Find("span")
    49  	b.StartTimer()
    50  	for i := 0; i < b.N; i++ {
    51  		if n == 0 {
    52  			n = sel.FindSelection(sel2).Length()
    53  		} else {
    54  			sel.FindSelection(sel2)
    55  		}
    56  	}
    57  	if n != 73 {
    58  		b.Fatalf("want 73, got %d", n)
    59  	}
    60  }
    61  
    62  func BenchmarkFindNodes(b *testing.B) {
    63  	var n int
    64  
    65  	b.StopTimer()
    66  	sel := DocW().Find("ul")
    67  	sel2 := DocW().Find("span")
    68  	nodes := sel2.Nodes
    69  	b.StartTimer()
    70  	for i := 0; i < b.N; i++ {
    71  		if n == 0 {
    72  			n = sel.FindNodes(nodes...).Length()
    73  		} else {
    74  			sel.FindNodes(nodes...)
    75  		}
    76  	}
    77  	if n != 73 {
    78  		b.Fatalf("want 73, got %d", n)
    79  	}
    80  }
    81  
    82  func BenchmarkContents(b *testing.B) {
    83  	var n int
    84  
    85  	b.StopTimer()
    86  	sel := DocW().Find(".toclevel-1")
    87  	b.StartTimer()
    88  	for i := 0; i < b.N; i++ {
    89  		if n == 0 {
    90  			n = sel.Contents().Length()
    91  		} else {
    92  			sel.Contents()
    93  		}
    94  	}
    95  	if n != 16 {
    96  		b.Fatalf("want 16, got %d", n)
    97  	}
    98  }
    99  
   100  func BenchmarkContentsFiltered(b *testing.B) {
   101  	var n int
   102  
   103  	b.StopTimer()
   104  	sel := DocW().Find(".toclevel-1")
   105  	b.StartTimer()
   106  	for i := 0; i < b.N; i++ {
   107  		if n == 0 {
   108  			n = sel.ContentsFiltered("a[href=\"#Examples\"]").Length()
   109  		} else {
   110  			sel.ContentsFiltered("a[href=\"#Examples\"]")
   111  		}
   112  	}
   113  	if n != 1 {
   114  		b.Fatalf("want 1, got %d", n)
   115  	}
   116  }
   117  
   118  func BenchmarkChildren(b *testing.B) {
   119  	var n int
   120  
   121  	b.StopTimer()
   122  	sel := DocW().Find(".toclevel-2")
   123  	b.StartTimer()
   124  	for i := 0; i < b.N; i++ {
   125  		if n == 0 {
   126  			n = sel.Children().Length()
   127  		} else {
   128  			sel.Children()
   129  		}
   130  	}
   131  	if n != 2 {
   132  		b.Fatalf("want 2, got %d", n)
   133  	}
   134  }
   135  
   136  func BenchmarkChildrenFiltered(b *testing.B) {
   137  	var n int
   138  
   139  	b.StopTimer()
   140  	sel := DocW().Find("h3")
   141  	b.StartTimer()
   142  	for i := 0; i < b.N; i++ {
   143  		if n == 0 {
   144  			n = sel.ChildrenFiltered(".editsection").Length()
   145  		} else {
   146  			sel.ChildrenFiltered(".editsection")
   147  		}
   148  	}
   149  	if n != 2 {
   150  		b.Fatalf("want 2, got %d", n)
   151  	}
   152  }
   153  
   154  func BenchmarkParent(b *testing.B) {
   155  	var n int
   156  
   157  	b.StopTimer()
   158  	sel := DocW().Find("li")
   159  	b.StartTimer()
   160  	for i := 0; i < b.N; i++ {
   161  		if n == 0 {
   162  			n = sel.Parent().Length()
   163  		} else {
   164  			sel.Parent()
   165  		}
   166  	}
   167  	if n != 55 {
   168  		b.Fatalf("want 55, got %d", n)
   169  	}
   170  }
   171  
   172  func BenchmarkParentFiltered(b *testing.B) {
   173  	var n int
   174  
   175  	b.StopTimer()
   176  	sel := DocW().Find("li")
   177  	b.StartTimer()
   178  	for i := 0; i < b.N; i++ {
   179  		if n == 0 {
   180  			n = sel.ParentFiltered("ul[id]").Length()
   181  		} else {
   182  			sel.ParentFiltered("ul[id]")
   183  		}
   184  	}
   185  	if n != 4 {
   186  		b.Fatalf("want 4, got %d", n)
   187  	}
   188  }
   189  
   190  func BenchmarkParents(b *testing.B) {
   191  	var n int
   192  
   193  	b.StopTimer()
   194  	sel := DocW().Find("th a")
   195  	b.StartTimer()
   196  	for i := 0; i < b.N; i++ {
   197  		if n == 0 {
   198  			n = sel.Parents().Length()
   199  		} else {
   200  			sel.Parents()
   201  		}
   202  	}
   203  	if n != 73 {
   204  		b.Fatalf("want 73, got %d", n)
   205  	}
   206  }
   207  
   208  func BenchmarkParentsFiltered(b *testing.B) {
   209  	var n int
   210  
   211  	b.StopTimer()
   212  	sel := DocW().Find("th a")
   213  	b.StartTimer()
   214  	for i := 0; i < b.N; i++ {
   215  		if n == 0 {
   216  			n = sel.ParentsFiltered("tr").Length()
   217  		} else {
   218  			sel.ParentsFiltered("tr")
   219  		}
   220  	}
   221  	if n != 18 {
   222  		b.Fatalf("want 18, got %d", n)
   223  	}
   224  }
   225  
   226  func BenchmarkParentsUntil(b *testing.B) {
   227  	var n int
   228  
   229  	b.StopTimer()
   230  	sel := DocW().Find("th a")
   231  	b.StartTimer()
   232  	for i := 0; i < b.N; i++ {
   233  		if n == 0 {
   234  			n = sel.ParentsUntil("table").Length()
   235  		} else {
   236  			sel.ParentsUntil("table")
   237  		}
   238  	}
   239  	if n != 52 {
   240  		b.Fatalf("want 52, got %d", n)
   241  	}
   242  }
   243  
   244  func BenchmarkParentsUntilSelection(b *testing.B) {
   245  	var n int
   246  
   247  	b.StopTimer()
   248  	sel := DocW().Find("th a")
   249  	sel2 := DocW().Find("#content")
   250  	b.StartTimer()
   251  	for i := 0; i < b.N; i++ {
   252  		if n == 0 {
   253  			n = sel.ParentsUntilSelection(sel2).Length()
   254  		} else {
   255  			sel.ParentsUntilSelection(sel2)
   256  		}
   257  	}
   258  	if n != 70 {
   259  		b.Fatalf("want 70, got %d", n)
   260  	}
   261  }
   262  
   263  func BenchmarkParentsUntilNodes(b *testing.B) {
   264  	var n int
   265  
   266  	b.StopTimer()
   267  	sel := DocW().Find("th a")
   268  	sel2 := DocW().Find("#content")
   269  	nodes := sel2.Nodes
   270  	b.StartTimer()
   271  	for i := 0; i < b.N; i++ {
   272  		if n == 0 {
   273  			n = sel.ParentsUntilNodes(nodes...).Length()
   274  		} else {
   275  			sel.ParentsUntilNodes(nodes...)
   276  		}
   277  	}
   278  	if n != 70 {
   279  		b.Fatalf("want 70, got %d", n)
   280  	}
   281  }
   282  
   283  func BenchmarkParentsFilteredUntil(b *testing.B) {
   284  	var n int
   285  
   286  	b.StopTimer()
   287  	sel := DocW().Find(".toclevel-1 a")
   288  	b.StartTimer()
   289  	for i := 0; i < b.N; i++ {
   290  		if n == 0 {
   291  			n = sel.ParentsFilteredUntil(":nth-child(1)", "ul").Length()
   292  		} else {
   293  			sel.ParentsFilteredUntil(":nth-child(1)", "ul")
   294  		}
   295  	}
   296  	if n != 2 {
   297  		b.Fatalf("want 2, got %d", n)
   298  	}
   299  }
   300  
   301  func BenchmarkParentsFilteredUntilSelection(b *testing.B) {
   302  	var n int
   303  
   304  	b.StopTimer()
   305  	sel := DocW().Find(".toclevel-1 a")
   306  	sel2 := DocW().Find("ul")
   307  	b.StartTimer()
   308  	for i := 0; i < b.N; i++ {
   309  		if n == 0 {
   310  			n = sel.ParentsFilteredUntilSelection(":nth-child(1)", sel2).Length()
   311  		} else {
   312  			sel.ParentsFilteredUntilSelection(":nth-child(1)", sel2)
   313  		}
   314  	}
   315  	if n != 2 {
   316  		b.Fatalf("want 2, got %d", n)
   317  	}
   318  }
   319  
   320  func BenchmarkParentsFilteredUntilNodes(b *testing.B) {
   321  	var n int
   322  
   323  	b.StopTimer()
   324  	sel := DocW().Find(".toclevel-1 a")
   325  	sel2 := DocW().Find("ul")
   326  	nodes := sel2.Nodes
   327  	b.StartTimer()
   328  	for i := 0; i < b.N; i++ {
   329  		if n == 0 {
   330  			n = sel.ParentsFilteredUntilNodes(":nth-child(1)", nodes...).Length()
   331  		} else {
   332  			sel.ParentsFilteredUntilNodes(":nth-child(1)", nodes...)
   333  		}
   334  	}
   335  	if n != 2 {
   336  		b.Fatalf("want 2, got %d", n)
   337  	}
   338  }
   339  
   340  func BenchmarkSiblings(b *testing.B) {
   341  	var n int
   342  
   343  	b.StopTimer()
   344  	sel := DocW().Find("ul li:nth-child(1)")
   345  	b.StartTimer()
   346  	for i := 0; i < b.N; i++ {
   347  		if n == 0 {
   348  			n = sel.Siblings().Length()
   349  		} else {
   350  			sel.Siblings()
   351  		}
   352  	}
   353  	if n != 293 {
   354  		b.Fatalf("want 293, got %d", n)
   355  	}
   356  }
   357  
   358  func BenchmarkSiblingsFiltered(b *testing.B) {
   359  	var n int
   360  
   361  	b.StopTimer()
   362  	sel := DocW().Find("ul li:nth-child(1)")
   363  	b.StartTimer()
   364  	for i := 0; i < b.N; i++ {
   365  		if n == 0 {
   366  			n = sel.SiblingsFiltered("[class]").Length()
   367  		} else {
   368  			sel.SiblingsFiltered("[class]")
   369  		}
   370  	}
   371  	if n != 46 {
   372  		b.Fatalf("want 46, got %d", n)
   373  	}
   374  }
   375  
   376  func BenchmarkNext(b *testing.B) {
   377  	var n int
   378  
   379  	b.StopTimer()
   380  	sel := DocW().Find("li:nth-child(1)")
   381  	b.StartTimer()
   382  	for i := 0; i < b.N; i++ {
   383  		if n == 0 {
   384  			n = sel.Next().Length()
   385  		} else {
   386  			sel.Next()
   387  		}
   388  	}
   389  	if n != 49 {
   390  		b.Fatalf("want 49, got %d", n)
   391  	}
   392  }
   393  
   394  func BenchmarkNextFiltered(b *testing.B) {
   395  	var n int
   396  
   397  	b.StopTimer()
   398  	sel := DocW().Find("li:nth-child(1)")
   399  	b.StartTimer()
   400  	for i := 0; i < b.N; i++ {
   401  		if n == 0 {
   402  			n = sel.NextFiltered("[class]").Length()
   403  		} else {
   404  			sel.NextFiltered("[class]")
   405  		}
   406  	}
   407  	if n != 6 {
   408  		b.Fatalf("want 6, got %d", n)
   409  	}
   410  }
   411  
   412  func BenchmarkNextAll(b *testing.B) {
   413  	var n int
   414  
   415  	b.StopTimer()
   416  	sel := DocW().Find("li:nth-child(3)")
   417  	b.StartTimer()
   418  	for i := 0; i < b.N; i++ {
   419  		if n == 0 {
   420  			n = sel.NextAll().Length()
   421  		} else {
   422  			sel.NextAll()
   423  		}
   424  	}
   425  	if n != 234 {
   426  		b.Fatalf("want 234, got %d", n)
   427  	}
   428  }
   429  
   430  func BenchmarkNextAllFiltered(b *testing.B) {
   431  	var n int
   432  
   433  	b.StopTimer()
   434  	sel := DocW().Find("li:nth-child(3)")
   435  	b.StartTimer()
   436  	for i := 0; i < b.N; i++ {
   437  		if n == 0 {
   438  			n = sel.NextAllFiltered("[class]").Length()
   439  		} else {
   440  			sel.NextAllFiltered("[class]")
   441  		}
   442  	}
   443  	if n != 33 {
   444  		b.Fatalf("want 33, got %d", n)
   445  	}
   446  }
   447  
   448  func BenchmarkPrev(b *testing.B) {
   449  	var n int
   450  
   451  	b.StopTimer()
   452  	sel := DocW().Find("li:last-child")
   453  	b.StartTimer()
   454  	for i := 0; i < b.N; i++ {
   455  		if n == 0 {
   456  			n = sel.Prev().Length()
   457  		} else {
   458  			sel.Prev()
   459  		}
   460  	}
   461  	if n != 49 {
   462  		b.Fatalf("want 49, got %d", n)
   463  	}
   464  }
   465  
   466  func BenchmarkPrevFiltered(b *testing.B) {
   467  	var n int
   468  
   469  	b.StopTimer()
   470  	sel := DocW().Find("li:last-child")
   471  	b.StartTimer()
   472  	for i := 0; i < b.N; i++ {
   473  		if n == 0 {
   474  			n = sel.PrevFiltered("[class]").Length()
   475  		} else {
   476  			sel.PrevFiltered("[class]")
   477  		}
   478  	}
   479  	// There is one more Prev li with a class, compared to Next li with a class
   480  	// (confirmed by looking at the HTML, this is ok)
   481  	if n != 7 {
   482  		b.Fatalf("want 7, got %d", n)
   483  	}
   484  }
   485  
   486  func BenchmarkPrevAll(b *testing.B) {
   487  	var n int
   488  
   489  	b.StopTimer()
   490  	sel := DocW().Find("li:nth-child(4)")
   491  	b.StartTimer()
   492  	for i := 0; i < b.N; i++ {
   493  		if n == 0 {
   494  			n = sel.PrevAll().Length()
   495  		} else {
   496  			sel.PrevAll()
   497  		}
   498  	}
   499  	if n != 78 {
   500  		b.Fatalf("want 78, got %d", n)
   501  	}
   502  }
   503  
   504  func BenchmarkPrevAllFiltered(b *testing.B) {
   505  	var n int
   506  
   507  	b.StopTimer()
   508  	sel := DocW().Find("li:nth-child(4)")
   509  	b.StartTimer()
   510  	for i := 0; i < b.N; i++ {
   511  		if n == 0 {
   512  			n = sel.PrevAllFiltered("[class]").Length()
   513  		} else {
   514  			sel.PrevAllFiltered("[class]")
   515  		}
   516  	}
   517  	if n != 6 {
   518  		b.Fatalf("want 6, got %d", n)
   519  	}
   520  }
   521  
   522  func BenchmarkNextUntil(b *testing.B) {
   523  	var n int
   524  
   525  	b.StopTimer()
   526  	sel := DocW().Find("li:first-child")
   527  	b.StartTimer()
   528  	for i := 0; i < b.N; i++ {
   529  		if n == 0 {
   530  			n = sel.NextUntil(":nth-child(4)").Length()
   531  		} else {
   532  			sel.NextUntil(":nth-child(4)")
   533  		}
   534  	}
   535  	if n != 84 {
   536  		b.Fatalf("want 84, got %d", n)
   537  	}
   538  }
   539  
   540  func BenchmarkNextUntilSelection(b *testing.B) {
   541  	var n int
   542  
   543  	b.StopTimer()
   544  	sel := DocW().Find("h2")
   545  	sel2 := DocW().Find("ul")
   546  	b.StartTimer()
   547  	for i := 0; i < b.N; i++ {
   548  		if n == 0 {
   549  			n = sel.NextUntilSelection(sel2).Length()
   550  		} else {
   551  			sel.NextUntilSelection(sel2)
   552  		}
   553  	}
   554  	if n != 42 {
   555  		b.Fatalf("want 42, got %d", n)
   556  	}
   557  }
   558  
   559  func BenchmarkNextUntilNodes(b *testing.B) {
   560  	var n int
   561  
   562  	b.StopTimer()
   563  	sel := DocW().Find("h2")
   564  	sel2 := DocW().Find("p")
   565  	nodes := sel2.Nodes
   566  	b.StartTimer()
   567  	for i := 0; i < b.N; i++ {
   568  		if n == 0 {
   569  			n = sel.NextUntilNodes(nodes...).Length()
   570  		} else {
   571  			sel.NextUntilNodes(nodes...)
   572  		}
   573  	}
   574  	if n != 12 {
   575  		b.Fatalf("want 12, got %d", n)
   576  	}
   577  }
   578  
   579  func BenchmarkPrevUntil(b *testing.B) {
   580  	var n int
   581  
   582  	b.StopTimer()
   583  	sel := DocW().Find("li:last-child")
   584  	b.StartTimer()
   585  	for i := 0; i < b.N; i++ {
   586  		if n == 0 {
   587  			n = sel.PrevUntil(":nth-child(4)").Length()
   588  		} else {
   589  			sel.PrevUntil(":nth-child(4)")
   590  		}
   591  	}
   592  	if n != 238 {
   593  		b.Fatalf("want 238, got %d", n)
   594  	}
   595  }
   596  
   597  func BenchmarkPrevUntilSelection(b *testing.B) {
   598  	var n int
   599  
   600  	b.StopTimer()
   601  	sel := DocW().Find("h2")
   602  	sel2 := DocW().Find("ul")
   603  	b.StartTimer()
   604  	for i := 0; i < b.N; i++ {
   605  		if n == 0 {
   606  			n = sel.PrevUntilSelection(sel2).Length()
   607  		} else {
   608  			sel.PrevUntilSelection(sel2)
   609  		}
   610  	}
   611  	if n != 49 {
   612  		b.Fatalf("want 49, got %d", n)
   613  	}
   614  }
   615  
   616  func BenchmarkPrevUntilNodes(b *testing.B) {
   617  	var n int
   618  
   619  	b.StopTimer()
   620  	sel := DocW().Find("h2")
   621  	sel2 := DocW().Find("p")
   622  	nodes := sel2.Nodes
   623  	b.StartTimer()
   624  	for i := 0; i < b.N; i++ {
   625  		if n == 0 {
   626  			n = sel.PrevUntilNodes(nodes...).Length()
   627  		} else {
   628  			sel.PrevUntilNodes(nodes...)
   629  		}
   630  	}
   631  	if n != 11 {
   632  		b.Fatalf("want 11, got %d", n)
   633  	}
   634  }
   635  
   636  func BenchmarkNextFilteredUntil(b *testing.B) {
   637  	var n int
   638  
   639  	b.StopTimer()
   640  	sel := DocW().Find("h2")
   641  	b.StartTimer()
   642  	for i := 0; i < b.N; i++ {
   643  		if n == 0 {
   644  			n = sel.NextFilteredUntil("p", "div").Length()
   645  		} else {
   646  			sel.NextFilteredUntil("p", "div")
   647  		}
   648  	}
   649  	if n != 22 {
   650  		b.Fatalf("want 22, got %d", n)
   651  	}
   652  }
   653  
   654  func BenchmarkNextFilteredUntilSelection(b *testing.B) {
   655  	var n int
   656  
   657  	b.StopTimer()
   658  	sel := DocW().Find("h2")
   659  	sel2 := DocW().Find("div")
   660  	b.StartTimer()
   661  	for i := 0; i < b.N; i++ {
   662  		if n == 0 {
   663  			n = sel.NextFilteredUntilSelection("p", sel2).Length()
   664  		} else {
   665  			sel.NextFilteredUntilSelection("p", sel2)
   666  		}
   667  	}
   668  	if n != 22 {
   669  		b.Fatalf("want 22, got %d", n)
   670  	}
   671  }
   672  
   673  func BenchmarkNextFilteredUntilNodes(b *testing.B) {
   674  	var n int
   675  
   676  	b.StopTimer()
   677  	sel := DocW().Find("h2")
   678  	sel2 := DocW().Find("div")
   679  	nodes := sel2.Nodes
   680  	b.StartTimer()
   681  	for i := 0; i < b.N; i++ {
   682  		if n == 0 {
   683  			n = sel.NextFilteredUntilNodes("p", nodes...).Length()
   684  		} else {
   685  			sel.NextFilteredUntilNodes("p", nodes...)
   686  		}
   687  	}
   688  	if n != 22 {
   689  		b.Fatalf("want 22, got %d", n)
   690  	}
   691  }
   692  
   693  func BenchmarkPrevFilteredUntil(b *testing.B) {
   694  	var n int
   695  
   696  	b.StopTimer()
   697  	sel := DocW().Find("h2")
   698  	b.StartTimer()
   699  	for i := 0; i < b.N; i++ {
   700  		if n == 0 {
   701  			n = sel.PrevFilteredUntil("p", "div").Length()
   702  		} else {
   703  			sel.PrevFilteredUntil("p", "div")
   704  		}
   705  	}
   706  	if n != 20 {
   707  		b.Fatalf("want 20, got %d", n)
   708  	}
   709  }
   710  
   711  func BenchmarkPrevFilteredUntilSelection(b *testing.B) {
   712  	var n int
   713  
   714  	b.StopTimer()
   715  	sel := DocW().Find("h2")
   716  	sel2 := DocW().Find("div")
   717  	b.StartTimer()
   718  	for i := 0; i < b.N; i++ {
   719  		if n == 0 {
   720  			n = sel.PrevFilteredUntilSelection("p", sel2).Length()
   721  		} else {
   722  			sel.PrevFilteredUntilSelection("p", sel2)
   723  		}
   724  	}
   725  	if n != 20 {
   726  		b.Fatalf("want 20, got %d", n)
   727  	}
   728  }
   729  
   730  func BenchmarkPrevFilteredUntilNodes(b *testing.B) {
   731  	var n int
   732  
   733  	b.StopTimer()
   734  	sel := DocW().Find("h2")
   735  	sel2 := DocW().Find("div")
   736  	nodes := sel2.Nodes
   737  	b.StartTimer()
   738  	for i := 0; i < b.N; i++ {
   739  		if n == 0 {
   740  			n = sel.PrevFilteredUntilNodes("p", nodes...).Length()
   741  		} else {
   742  			sel.PrevFilteredUntilNodes("p", nodes...)
   743  		}
   744  	}
   745  	if n != 20 {
   746  		b.Fatalf("want 20, got %d", n)
   747  	}
   748  }
   749  
   750  func BenchmarkClosest(b *testing.B) {
   751  	var n int
   752  
   753  	b.StopTimer()
   754  	sel := Doc().Find(".container-fluid")
   755  	b.StartTimer()
   756  	for i := 0; i < b.N; i++ {
   757  		if n == 0 {
   758  			n = sel.Closest(".pvk-content").Length()
   759  		} else {
   760  			sel.Closest(".pvk-content")
   761  		}
   762  	}
   763  	if n != 2 {
   764  		b.Fatalf("want 2, got %d", n)
   765  	}
   766  }
   767  
   768  func BenchmarkClosestSelection(b *testing.B) {
   769  	var n int
   770  
   771  	b.StopTimer()
   772  	sel := Doc().Find(".container-fluid")
   773  	sel2 := Doc().Find(".pvk-content")
   774  	b.StartTimer()
   775  	for i := 0; i < b.N; i++ {
   776  		if n == 0 {
   777  			n = sel.ClosestSelection(sel2).Length()
   778  		} else {
   779  			sel.ClosestSelection(sel2)
   780  		}
   781  	}
   782  	if n != 2 {
   783  		b.Fatalf("want 2, got %d", n)
   784  	}
   785  }
   786  
   787  func BenchmarkClosestNodes(b *testing.B) {
   788  	var n int
   789  
   790  	b.StopTimer()
   791  	sel := Doc().Find(".container-fluid")
   792  	nodes := Doc().Find(".pvk-content").Nodes
   793  	b.StartTimer()
   794  	for i := 0; i < b.N; i++ {
   795  		if n == 0 {
   796  			n = sel.ClosestNodes(nodes...).Length()
   797  		} else {
   798  			sel.ClosestNodes(nodes...)
   799  		}
   800  	}
   801  	if n != 2 {
   802  		b.Fatalf("want 2, got %d", n)
   803  	}
   804  }
   805  
   806  func BenchmarkSingleMatcher(b *testing.B) {
   807  	doc := Doc()
   808  	multi := cascadia.MustCompile(`div`)
   809  	single := SingleMatcher(multi)
   810  	b.ResetTimer()
   811  
   812  	b.Run("multi", func(b *testing.B) {
   813  		for i := 0; i < b.N; i++ {
   814  			_ = doc.FindMatcher(multi)
   815  		}
   816  	})
   817  	b.Run("single", func(b *testing.B) {
   818  		for i := 0; i < b.N; i++ {
   819  			_ = doc.FindMatcher(single)
   820  		}
   821  	})
   822  }
   823  

View as plain text